Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Calling external program in java code

Reply
Thread Tools

Calling external program in java code

 
 
hrn
Guest
Posts: n/a
 
      09-08-2009
Hi,

I have problems with this code fragment.
..
..
..
String exe = "external1.exe";
String exe2 = "external2.exe";
Process rt = Runtime.getRuntime().exec(exe1);
rt.waitFor();
System.out.println("Finished executing external program");
..
..
When I run this code, it runs perfectly, but when I changed third line
to [ Process rt = Runtime.getRuntime().exec(exe2); ] it never stops.
It holds at fourth line [ rt.waitFor(); ] and fifth line is never
executed. Windows Task Manager shows that external2.exe is still
running. When I call both external1.exe and external2.exe from command
line, they run and stop in expected way and they don't need any user
intervention to work and stop at the end. I need help.

Thanks in advance.

 
Reply With Quote
 
 
 
 
Daniel Pitts
Guest
Posts: n/a
 
      09-08-2009
hrn wrote:
> Hi,
>
> I have problems with this code fragment.
> ..
> ..
> ..
> String exe = "external1.exe";
> String exe2 = "external2.exe";
> Process rt = Runtime.getRuntime().exec(exe1);
> rt.waitFor();
> System.out.println("Finished executing external program");
> ..
> ..
> When I run this code, it runs perfectly, but when I changed third line
> to [ Process rt = Runtime.getRuntime().exec(exe2); ] it never stops.
> It holds at fourth line [ rt.waitFor(); ] and fifth line is never
> executed. Windows Task Manager shows that external2.exe is still
> running. When I call both external1.exe and external2.exe from command
> line, they run and stop in expected way and they don't need any user
> intervention to work and stop at the end. I need help.
>
> Thanks in advance.
>

is it possible that external2.exe is either waiting for input, or
outputting more data than external1?

There is a finite buffer used for Process's output/error streams, so you
*must* drain them (using a separate thread is a common practice, but not
necessary). If the output or error stream become full, the process will
block until they are drained.

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
 
Reply With Quote
 
 
 
 
hrn
Guest
Posts: n/a
 
      09-08-2009
On windows command line, any user input is not necessary after running
external1 and external2. For both of them, I just write their names
and their arguments (these arguments are names of files that will be
processed) on command line and push enter button, then I do nothing,
they work and stop succesfully. Although, I write the same (what I
write on command line) in java as exe and exe2, exe works succesfully,
exe2 waits.
 
Reply With Quote
 
Martin Gregorie
Guest
Posts: n/a
 
      09-08-2009
On Tue, 08 Sep 2009 07:15:45 -0700, hrn wrote:

> Hi,
>
> I have problems with this code fragment. .
> .
> .
> String exe = "external1.exe";
> String exe2 = "external2.exe";
> Process rt = Runtime.getRuntime().exec(exe1); rt.waitFor();
> System.out.println("Finished executing external program"); .
> .
> When I run this code, it runs perfectly, but when I changed third line
> to [ Process rt = Runtime.getRuntime().exec(exe2); ] it never stops.
>

As written, a class containing these fragments ought not to compile, let
alone run because exe1 is never declared. Has anything else been changed
or omitted?

Create an SSCE that does compile and run and post that. A similar SSCE
for each of the called programs would be even better if it can
demonstrate the problem.


--
martin@ | Martin Gregorie
gregorie. | Essex, UK
org |
 
Reply With Quote
 
hrn
Guest
Posts: n/a
 
      09-08-2009
Actually, in my java program, exe is exe1. 1 is missing here. There
has been a mistake while writing.

 
Reply With Quote
 
Martin Gregorie
Guest
Posts: n/a
 
      09-08-2009
On Tue, 08 Sep 2009 13:45:17 -0700, hrn wrote:

> Actually, in my java program, exe is exe1. 1 is missing here. There has
> been a mistake while writing.


Use copy & paste next time. However, a compiled and tested SSCE would be
better.


--
martin@ | Martin Gregorie
gregorie. | Essex, UK
org |
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      09-09-2009
On Tue, 8 Sep 2009 07:15:45 -0700 (PDT), hrn <(E-Mail Removed)>
wrote, quoted or indirectly quoted someone who said :

>.
>String exe = "external1.exe";
>String exe2 = "external2.exe";
>Process rt = Runtime.getRuntime().exec(exe1);
>rt.waitFor();
>System.out.println("Finished executing external program");


You have constipation. You must allow for the flow of output from the
child. See http://mindprod.com/jgloss/exec.html
--
Roedy Green Canadian Mind Products
http://mindprod.com

"I mean, source code in files; how quaint, how seventies!"
~ Kent Beck (born: 1961 age: 4, evangelist for extreme programming.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling external program from within python Emmanouil Angelakis Python 10 07-25-2008 09:53 PM
Calling external program from browser with parameters - cross-browser Jeff ASP .Net 13 10-06-2007 08:03 AM
calling external program from within C++ Juggler C Programming 5 02-24-2005 07:04 PM
Calling external program in C++ Jon Slaughter C++ 9 12-15-2004 03:07 PM
Calling Java program in another Java program Rey Java 4 12-12-2003 10:18 PM



Advertisments