Blocking vs. Non-Blocking Calls

suggest change

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

You may instead want to wait until the called program finishes. For example, it writes a file that your program then reads. If the child fills its stdout or stderr buffer and nothing reads those streams, waitFor() can wait forever.

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.printStackTrace();
} catch (InterruptedException e) {
    e.printStackTrace();
}

//more lines of code here...

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents