waitFor() Help

S

sandancer

Hi,

I'm a project manager and a developer in my team is writing some java
code to run an exe that is part of a vendor application we are
implementing. The developer is claiming there is problems with the
vendors exe as it just return the value 259 constantly for exitValue -
even when he can see it's finished i.e. it hangs returning this value
in his code when he can see it's done. The exe sits on a Windows 2003
server and the code snippet is:

// Runs the batch job
Process p = Runtime.getRuntime().exec("path-to\vendor-app.exe
-batch=EXEQHOLD");

// Cause the current Thread to wait until an exit value is returned.
// The convention for normal termination is zero
int exitValue = p.waitFor();

I was a programmer in a past life but not java but my gut feeling is
that maybe we should be more thorough with this before we go back to
blaming the vendor. Can anyone comment on this or suggest a better way
to launch an exe and wait for it in code to finish?

Many thanks,

s
 
L

Larry Barowski

sandancer said:
Hi,

I'm a project manager and a developer in my team is writing some java
code to run an exe that is part of a vendor application we are
implementing. The developer is claiming there is problems with the
vendors exe as it just return the value 259 constantly for exitValue -
even when he can see it's finished i.e. it hangs returning this value
in his code when he can see it's done.
...
I was a programmer in a past life but not java but my gut feeling is
that maybe we should be more thorough with this before we go back to
blaming the vendor. ...

Just run the program from the command prompt and check
the exit value (echo %ERRORLEVEL%). If it is always 259,
then the problem is not yours.
 
J

John C. Bollinger

sandancer said:
I'm a project manager and a developer in my team is writing some java
code to run an exe that is part of a vendor application we are
implementing. The developer is claiming there is problems with the
vendors exe as it just return the value 259 constantly for exitValue -
even when he can see it's finished i.e. it hangs returning this value
in his code when he can see it's done. The exe sits on a Windows 2003
server and the code snippet is:

// Runs the batch job
Process p = Runtime.getRuntime().exec("path-to\vendor-app.exe
-batch=EXEQHOLD");

// Cause the current Thread to wait until an exit value is returned.
// The convention for normal termination is zero
int exitValue = p.waitFor();

I was a programmer in a past life but not java but my gut feeling is
that maybe we should be more thorough with this before we go back to
blaming the vendor. Can anyone comment on this or suggest a better way
to launch an exe and wait for it in code to finish?

If the code you showed is all there is to it then it is a bit
simplistic. It should work if the program doesn't produce *any* output
on its standard output or standard error streams, but may otherwise
block when its I/O buffers fill. (I'm assuming that since it's a batch
job, it doesn't read anything from its standard input.) If the external
program only produces a small amount of output then it might run to
completion, but never exit. To address both of these potential
problems, a Java program should generally arrange to drain any external
process's output concurrently with the process; the data are available
via the Process object's getInputStream() and getErrorStream() methods,
and separate threads should be established to read each.

The return value of Process.exitValue() is undefined until the external
process has terminated. You can block on termination of the process via
Process.waitFor(), as you show in your example code.
 

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

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top