Implement wait between processes in Java?

I would like some help to understand and implement “wait for the process to complete” between the various processes in my application, which should be phased. My java file runs the batch file, which then runs the script. In conclusion, there are a number of commands that I need to execute (via the command line) sequentially. I use:

Runtime.getRuntime().exec("cmd /c start " + command)

to run my batch files and commands (not sure if this information is relevant). Currently, what happens is that the second step, which should happen in my application, is performed before the first step (launching the batch file that launches the script). I need to take the first step to complete the next series of commands. I really hope I make sense!

+3
source share
3 answers

exec () returns an instance of the process on which you can do waitFor ().

Beware: I think that "start" will actually spawn a separate Windows process, so waitFor () may return before the command completes. Try to remove "start" from the command line?

+6
source

Easily:

just call the waitFor () method of your Process instance. It will stop the thread until the external process is complete;

+3
source

?

ProcessBuilder, Process waitFor() :

, , , , Process, .

EDIT: Actually exec()returns Process, so you don't need to worry with the part ProcessBuilder.

+1
source

Source: https://habr.com/ru/post/1704893/


All Articles