Java.lang.Process redirects error to / dev / null

I started the process (via java.lang.Runtime # exec, in Java 6, Linux), for which I only need stdout. Unfortunately, the program I am running (ffmpeg) insists on stderr cluttering up with progress information. If I do not read from stderr from time to time, the stderr buffer is full, and the process is delayed after a while.

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

1)

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

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

2)

Use the ffmpeg command above in a shell script that redirects output to / dev / null. Sounds fine, but has a bash script only for what seems redundant.

3)

Attach ErrorStream, run a thread that does nothing but read () in the error stream. It will work, but it looks randomly ....

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

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

+4
source share
3 answers

Of these options, number 3 β€” creating a stream to read the error stream β€” is probably the best.

Writing your own analyzer will be enough work and will become an unnecessary source of errors. Using a script wrapper creates an additional unnecessary dependency, that is another potential source of problems. I don’t see to use the new library when you already have a solution that is just a few lines of simple code.

+2
source

Instead of Apache Commons Exec, you can also use Overthere . It supports (local) process execution. And consumes both stdout and stderr for you.

0
source

You can use ProcessBuilder and redirect (redirectErrorStream (true)) stderr to stdout, then you only need to read the output of one stream. This can make any parsing of the output you make a little more complicated. One of the advantages is that it is a Java class that does not require external libraries.

0
source

Source: https://habr.com/ru/post/1403054/


All Articles