/ java

Follow up on the Process API in Java

Photo by Garett Mizunaka on Unsplash

In my last blog post, I focused on the improvements made in the process API and proposed an alternative implementation, for a more fine grained control on the threads executing waitFor.

It appears that after digging, I realized that a process may be blocked if it does not drain its output stream. There is tons of blog posts about this subject. If you are not interested in the output and error output (stderr and stdout), you can redirect them to /dev/null easily with the ProcessBuilder API (since Java 7) :

new ProcessBuilder().redirectErrorStream(true).redirectOutput(new File("/dev/null"));

This simple code will redirect the error stream into the output stream (merging stderr and stdout) and will write to /dev/null, which will behave as a black hole.

Then, to better handle the output and error of an external process, either redirect explicitly to a file with the ProcessBuilder API, or create a mechanism to drain the output/error.

Emmanuel Nhan

30 years old French software developer. I started programming in C, just for fun when I was 13. Then I jumped in several techs to end up writing Scala code and enjoying DIY stuffs like 3D printing :)

Read More