Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can't grep on 'java -version' output [duplicate]

Tags:

java

I'm trying to grep on output from the commands:

java -version
java -XshowSettings

but it seems like they refuse to be redirected or piped.

I tried

java -version | grep whatever
java -version > jout.txt

but both just print the output to the screen.

What is going on?

Thanks, Gilad.

like image 290
giladrv Avatar asked Nov 16 '14 07:11

giladrv


1 Answers

You need to redirect to stdout before you can pipe like that. Those messages go to stderr by default, rather than stdout; that means that grep won't see the messages, and they'll just get printed to the console.

If this is Linux, try

java -version 2>&1 | grep whatever

and it should work. This will take all output to stderr from the java execution, and redirect it so that it goes to the same place as stdout; your grep invocation will then be able to see it.

like image 52
chiastic-security Avatar answered Oct 07 '22 14:10

chiastic-security