Cannot grep the output of "java -version"

I try grep on the output from the commands:

java -version java -XshowSettings 

but it looks like they refuse to redirect or redirect.

I tried

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

but both just print the output to the screen.

What's happening?

Thanks, Gilad.

+5
source share
1 answer

You need to redirect to stdout before you can do this. These messages are sent to stderr by default, not to stdout; this means that grep will not see the messages and they will simply be printed to the console.

If it is Linux, try

 java -version 2>&1 | grep whatever 

and it should work. This will bring all the output to stderr from java execution and redirect it so that it moves to the same place as stdout; your grep call will be able to see it.

+16
source

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


All Articles