Java Language Process Blocking vs. Non-Blocking Calls


Example

In general when making a call to the command line, the program will send the command and then continue its execution.

However you may want to wait for the called program to finish before continuing your own execution (ex. The called program will write data to a file and your program needs that to access that data.)

This can easily be done by calling the waitFor() method from the returned Process instance.

Usage example:

//code setting up the commands omitted for brevity...

ProcessBuilder pb = new ProcessBuilder(cmds);

try {
    Process p = pb.start();
    p.waitFor();
} catch (IOException e) {
    e.printStackTrack();
} catch (InterruptedException e) {
    e.printStackTrace();
}

//more lines of code here...