From: hrn on
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.

From: Daniel Pitts on
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/>
From: Stefan Weiss on
On 08/09/09 16:15, hrn wrote:
> 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.

I would also guess that exe2 is waiting for input - perhaps it's trying
to read from STDIN (System.in) but not receiving anything. Some programs
will do that if they're called without a specific parameter, for example

more textfile (does its thing)

versus

more (waits)

Some programs will also check if STDIN or STDOUT are connected to a
terminal; this means they can behave in a different way when called
interactively on the commandline. I'm not very familiar with the Windows
environment, but in Unix-land this is a pretty common situation. You
could test for this on the commandline by piping the output of
external2.exe to another program, or sending it the output of another
program. If that causes external2 to hang or wait, your problem is the
behavior of external2.


cheers,
stefan
From: hrn on
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.
From: Martin Gregorie on
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 |