I want to fetch the Java version in Linux in a single command.
I am new to awk so I am trying something like
java -version|awk '{print$3}'
But that does not return the version. How would I fetch the 1.6.0_21
from the below Java version output?
java version "1.6.0_21"
Java(TM) SE Runtime Environment (build 1.6.0_21-b06)
Java HotSpot(TM) 64-Bit Server VM (build 17.0-b16, mixed mode)
Filter the version number.
This way works for me.
This is a slight variation, but PJW's solution didn't quite work for me:
just cut the string on the delimiter
"
(double quotes) and get the second field.Getting only the "major" build #:
Since (at least on my linux system) the version string looks like "1.8.0_45":
I'd suggest using
grep -i version
to make sure you get the right line containing the version string. If you have the environment variable JAVA_OPTIONS set, openjdk will print the java options before printing the version information. This version returns 1.6, 1.7 etc.