Child process won't end or end on Windows?

How do I make my parent java process wait for the child process to finish. I've tried with runtime.exec

and processBuilder.pb

:

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

      


This works great with a small input file (say less than 10MB). If I give a larger input file, then the program will hang. The output file will be partially created and the file creation will be modified and control will not return. proc.join(10000);

Didn't even give any useful result. Here the parent process ends before the child process (ffmpeg) terminates.

How to solve this problem?

+3


source to share


1 answer


You need to read the data output by the process.

The process has 2 output streams: standard output and error output. You must read both, because the process can write to both of these outputs.

The output streams of the process have buffers. If the output stream buffer is full, an attempt to write additional data to this 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);
}

      

+3


source







All Articles