Karthikeya Vaidya
Karthikeya Vaidya

Reputation: 131

Child Process will neither complete nor abort in Windows?

How to make my java parent process wait till child process gets completed. I have tried with runtime.exec and with processBuilder.pb:

String cmd = "ffmpeg -i input.vob output.mp4" 
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(cmd); 
proc.waitFor();

This works fine with small input file (say less than 10 Mb). If I give larger input file then program will be hanged. Output file will be partially created and file creation will be hanged and control will not return. Even proc.join(10000); did not give any useful result. Here parent process is terminating before child process (ffmpeg) gets completed.

How to overcome this problem?

Upvotes: 3

Views: 133

Answers (1)

icza
icza

Reputation: 418655

You need to read the data outputted by the process.

A process has 2 output streams: standard output and error output. You have to read both because the process might write to both of those outputs.

The output streams of the process have buffers. If the buffer of an output stream is filled, attempt to write further data to that by the process is blocked.

Do something like this:

InputStream in = proc.getInputStream();  // To read process standard output
InputStream err = proc.getErrorStream(); // To read process error output

while (proc.isAlive()) {
    while (in.available() > 0)
        in.read(); // You might wanna echo it to your console to see progress

    while (err.available() > 0)
        err.read(); // You might wanna echo it to your console to see progress

    Thread.sleep(1);
}

Upvotes: 3

Related Questions