I am calling a command line program from java using:
Process child = Runtime.getRuntime().exec(cmd);
where cmd is a string (the command I want to execute). I want to capture
the command's exit value. So I then issue a
i = child.waitFor();
The documentation says that waitfor() "Waits for the subprocess to complete"
and returns "the exit value of the process". The problem is that sometimes
the child.waitFor() returns 0 when it should have been 1. I have determined
that the problem is child.waitFor() returning before Runtime.getRuntime().exec
has completed. I found this out by doing:
child.waitFor();
i = child.exitValue();
instead of
i = child.waitFor()
I then get the following error (intermittently):
java.lang.IllegalThreadStateException: process hasn't exited
at java.lang.UNIXProcess.exitValue(UNIXProcess.java)
at ExecCmd.run(ExecCmd.java:39)
at TapeReq.run(TapeReq.java:149)
I have come up with a clumsy work around by itterating through a while loop
if I get an IllegalThreadStateException. Then when
child.waitFor(); i=child.exitValue();
does not return an IllegalThreadStateException, I return the value of i.
I am hoping that someone can tell me a better way to do this. Am I
using the waitFor() method incorrectly? Any help is greatly appreciated.
--
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Andy Kowalski | _/_/_/_/_/ _/_/_/_/_/ _/ _/ _/_/_/_/ _/_/_/_/
| _/ _/ _/_/ _/ _/ _/ _/
kowa...@cebaf.gov | _/ _/ _/ _/ _/ _/_/_/_/ _/_/
| _/ _/ _/ _/ _/_/ _/ _/ _/
Phone: 757.269.6224 | _/ _/_/_/ _/ _/ _/ _/ _/
FAX: 757.269.7053 | TJNAF - Thomas Jefferson National Accelerator Facility
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=