Shadi
Shadi

Reputation: 689

cause the main process wait until another process is finished

I have a synchronization problem in java. I want my main thread to wait until process "p1" is finished. I have used "waitfor" method. it has not worked for me.

Process p1 = runtime.exec("cmd /c start /MIN " + path + "aBatchFile.bat" );
p1.waitFor();

Could anybody help me please?

Thank you so much.

Upvotes: 1

Views: 3872

Answers (1)

Greg Hewgill
Greg Hewgill

Reputation: 992787

The problem here is that the Process object you get back from exec() represents the instance of cmd.exe that you start. Your instance of cmd.exe does one thing: it starts a batch file and then exits (without waiting for the batch file, because that's what the start command does). At that point, your waitFor() returns.

To avoid this problem, you should be able to run the batch file directly:

Process p1 = runtime.exec(path + "aBatchFile.bat");
p1.waitFor();

Alternately, try the /wait command line option:

Process p1 = runtime.exec("cmd /c start /wait /MIN " + path + "aBatchFile.bat" ); 
p1.waitFor(); 

Upvotes: 4

Related Questions