Blog-Archiv

Freitag, 7. November 2025

Badly Documented Java Process Handling Problem

For a long time I used VLC as video player, until I got bored by the fact that it froze after a while and had to be terminated using the operating system monitor. That was the reason why I tried out MPV, which is nearer to the (in)famous ffmpeg video library (used by almost ever web-browser). But it was the same, even worse, MPV froze after only two videos.

Now that made me think. Mostly I used my own Java file-browser to play my hiking video-clips videos. Maybe the problem are not those video players and the hundreds of clips I pass to them as parameters, but it is some Java restriction to a started process? So I began to search on stackoverflow for such problems and found hints:

Looks like you have to ALWAYS read away any process output! This would also explain why MPV blocked faster than VLC, because the first writes lots of logging to the console.

Here is the Java code of my bug-fix. Mind that this dismisses any error or normal output of the process. First, how to start a process from Java:

    final String[] command = ....
    final String[] environment = ....
    final File workingDirectory = ....
    final Process p =  Runtime.getRuntime().exec(command, environment, workingDirectory);
    readAwayProcessOutput(p.getInputStream());
    readAwayProcessOutput(p.getErrorStream());

Mind that getInputStream() is the meaning how seen from the process starter perspective, i.e. the stream delivers what the started process sends to the starter. The getOutputStream() method is for writing to the started process.

Here is how to read away input-streams, whatever they deliver:

    private void readAwayProcessOutput(InputStream in) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    while (in.read() != -1) // -1 is end of stream
                        ;
                    in.close();
                }
                catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();
    }

This is something that is not documented so explicitly that a programmer would see it. Read it here:

It says:

....may cause the subprocess to block, or even deadlock.

Can we call it a communication problem:-?