how to capture the java version on linux redhat machine

we tried that:

java -version | head -1 | awk '{print $NF}' | sed 's/"//g'

but we get

openjdk version "1.8.0_65"
OpenJDK Runtime Environment (build 1.8.0_65-b17)
OpenJDK 64-Bit Server VM (build 25.65-b01, mixed mode)

while expected output should be


java -version outputs to its standard error, so you need to redirect that:

java -version 2>&1 | head -1 | awk '{print $NF}' | sed 's/"//g'

You can do this with a single AWK invocation:

java -version 2>&1 | awk 'NR==1 {gsub("\"", "", $NF); print $NF}'
  • we can also do it on remote machine as ssh $IP "java -version 2>&1 | head -1 | awk '{print \$NF}' | sed 's/\"//g' " – yael Apr 26 '18 at 14:30
  • well done step , this is one of the best answer I have seen – yael Apr 26 '18 at 14:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.