Claude
Claude

Reputation: 9980

Java.lang.Process redirect errorstream to /dev/null

I started a Process (through java.lang.Runtime#exec, on Java 6, Linux), for which I only need the stdout. Unfortunately the program I run (ffmpeg) insists on cluttering up the stderr with progress information. If I don't read from stderr every now and then, the stderr buffer fills up, and the process stalls after a while..

Basically what I want is to discard any output from stderr. My suggestions

1)

ffmpeg -i .... .... 2>/dev/null

This works, but means I have to exec(String) in stead of exec(String[]), which means I have to escape my parameters, for which there is no standard function in Java. I could build one, but prefer not.

2)

Use the above ffmpeg command in a wrapper script, that redirects the output to /dev/null. Sounds ok, but having a bash script just for that seems overkill.

3)

Attach the ErrorStream as well, start a thread that does nothing but read() on the errorstream. Will work, but looks messy....

4) Use Apache Commons Exec... I didn't even check the documentation to see if this will work, but importing that whole library just for such a simple task doesn't feel right either.

So basically my question is: is there a better way to do this? If not, which one would you consider [strike]most beautiful[/strike] least ugly?

Upvotes: 4

Views: 2391

Answers (3)

David Webb
David Webb

Reputation: 193716

Of those options, Number 3 - creating a Thread to read the error stream - is probably the best.

Writing your own parser is going to be a fair amount of work and will be an unnecessary source of bugs. Using a wrapper script creates an extra unnecessary dependency, i.e. another potential source of problems. I don't see to use a new library when you already have a solution which is only a few lines of simple code.

Upvotes: 2

basszero
basszero

Reputation: 30014

You could use ProcessBuilder and redirect ( redirectErrorStream(true) ) the stderr to stdout then you only have to read the output of one stream. This may make any output parsing you were doing a little bit harder. One benefit is that this is a Java class, no external libs needed.

Upvotes: 0

Hiery Nomus
Hiery Nomus

Reputation: 17769

Instead of Apache Commons Exec you could also use Overthere. It support (local) process execution. And gobbles the both the stdout and stderr for you.

Upvotes: 0

Related Questions