Why does Process.waitFor() never return?

10,789

This is an OS thing. The child process is writing to stdout, and that's being buffered waiting for your Java process to read it. When you don't read it, the buffer eventually fills up and the child process blocks writing to stdout waiting for buffer space.

You would have to processes the child process' stdout (and stderr) whichever language you were using.

I suggest you read this article (all 4 pages of it) and implement the recommendations there.

Share:
10,789
Manuel Selva
Author by

Manuel Selva

Computer scientist

Updated on June 04, 2022

Comments

  • Manuel Selva
    Manuel Selva almost 2 years

    I am launching a windows process (wrote in C++ but I don't have sources) from Java code in the following way:

     Process p1 = Runtime.getRuntime().exec(cmdAndParams);
     p1.waitFor();
    

    My problem is that the waitFor() method never ends. Thus I tried to launch the process in a simple shell and it ends correctly with many print in the shell (standard output I guess).

    Thus I decided to create and start a thread reading the standard output even if I don't need for now these outputs. This fixed the issue.

    So my question is the following one: Is this solution the "Java standard to launch and wait for external processes having outputs" or does it means there is an issue somewhere in the native process ? If such an issue exist what C++ programming "error" can be at the origin of the issue ?

  • Manuel Selva
    Manuel Selva over 13 years
    Thanks. The answer was in the Javadoc but at class level and I only read the exec and waitFor methods doc. Next time I'll also read class doc ;-)
  • Arman
    Arman about 3 years
    Unfortunately, the article doesn't exist anymore. At least not under the posted url.