Runtime.Exec with cmd and problem in waitFor

Hi

I am running

Process p1 = Runtime.getRuntime().exec("cmd /c start /min <some.exe> <args to exe>");

then I am checking

Int ExitVal = p1.waitFor;

If (ExitVal != 0) and so on.

However, the problem is that the p1.waitFor does not really block the thread. It returns 0 and the program continues even though the some.exe has still not completed its execution.

My guess is that Java is giving the exitcode of cmd.exe and not some.exe.

If this analysis is correct, I am stumped. How do I get the exit code of some.exe?

If my analysis is wrong, please help me in the right direction.

Regards

Shreekar

[682 byte] By [Shreekara] at [2007-10-2 21:56:06]
# 1
Start your application without additional tools like cmd or start. Just Runtime.exec( "some.exe" );
Michael.Nazarov@sun.coma at 2007-7-14 1:12:09 > top of Java-index,Java Essentials,Java Programming...
# 2
I guess (s)he's doing so because of the "/min" option for execution in minimized mode.
quittea at 2007-7-14 1:12:09 > top of Java-index,Java Essentials,Java Programming...
# 3

Hi !

I am not a windows man so maybe I am wrong but as I remember the start command sends the execution in the background so the cmd program will exit as soon as this is done and with a return code of 0. On Linux one has similar with the & at the end of a command.

So to wait you have to remove the start keyword.

ostensea at 2007-7-14 1:12:09 > top of Java-index,Java Essentials,Java Programming...
# 4
Yes, I have to use cmd because of min option.and it is a he :)I am testing by removing the start keyword - will post what happens.Message was edited by: Shreekar
Shreekara at 2007-7-14 1:12:09 > top of Java-index,Java Essentials,Java Programming...
# 5

Thanks ostense...after removing the \start and \min switches, the thread is blocked and I am getting correct results.

However, the start switch means something else...

See http://www.computerhope.com/cmd.htm

and

http://www.computerhope.com/starthlp.htm

I dont understand why removing these switches made Java a good boy.

Meanwhile, I am testing the side effects of NOT using the min switch :)

Regards

Shreekar

Shreekara at 2007-7-14 1:12:09 > top of Java-index,Java Essentials,Java Programming...
# 6

Cf the switch /WAIT and the implication of its absence:

start /?

START ["title"] [/Dpath] [/MIN] [/MAX] [/SEPARATE | /SHARED]

[/LOW | /NORMAL | /HIGH | /REALTIME | /ABOVENORMAL | /BELOWNORMAL]

[/WAIT] [command/program]

[parameters]

WAITStart application and wait for it to terminate

BIJ001a at 2007-7-14 1:12:09 > top of Java-index,Java Essentials,Java Programming...
# 7
It seems like the start command starts a new shell (window) which has the same effect as sending it to background ( Linux term) . From the link you gave it explains the start command :"Enables a user to start a separate window in Windows from the MS-DOS prompt."
ostensea at 2007-7-14 1:12:09 > top of Java-index,Java Essentials,Java Programming...
# 8
> which has the same effect as sending it to background ( Linux term) .Actually under Linux undependently whether you send it into background or not, it will not obtain a new terminal window. & at the end makes the starting shell not to wait for the spawned process.
BIJ001a at 2007-7-14 1:12:09 > top of Java-index,Java Essentials,Java Programming...
# 9
> Cf the switch /WAIT and the implication of its> WAITStart application and wait for it to> terminateYes, I read the fine print atterwards :) I will testing by putting Start /min /wait and will post the results.
Shreekara at 2007-7-14 1:12:09 > top of Java-index,Java Essentials,Java Programming...
# 10

> "Enables a user to start a separate window in Windows

> from the MS-DOS prompt."

Again, I read it afterwards. However, another funny thing is happening now. Initially my command was

"cmd start /min (some.exe) (some args) > somefile.txt"

(I know I missed to show the output redirection in my original post.)

Then I removed "start /min", it is working as expected.

Then I removed the output redirection and now it hangs !!!

I put back the output redirection and it is working. Does the output redirection make it wait in some way and force it to exit gracefully?

Any ideas?

Shreekara at 2007-7-14 1:12:09 > top of Java-index,Java Essentials,Java Programming...
# 11
All output should be readen or redirected somethere. If you not care about output then output buffer will be filled out and child program will hang.
Michael.Nazarov@sun.coma at 2007-7-14 1:12:09 > top of Java-index,Java Essentials,Java Programming...