Calling external program in java code

H

hrn

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.
 
D

Daniel Pitts

hrn said:
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.
 
H

hrn

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.
 
M

Martin Gregorie

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.
 
H

hrn

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

Martin Gregorie

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.
 
R

Roedy Green

.
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: 48), evangelist for extreme programming.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top