Actually, the output format of "java -version" is quite formatted:
IBM JDK 1.6:
java version "1.6.0"
Java(TM) SE Runtime Environment (build pxa6460sr7-20091215_02(SR7))
IBM J9 VM (build 2.4, JRE 1.6.0 IBM J9 2.4 Linux amd64-64 jvmxa6460sr7-20091214_49398 (JIT enabled, AOT enabled)
J9VM - 20091214_049398
JIT - r9_20091123_13891
GC - 20091111_AA)
JCL - 20091202_01
OpenJDK 1.7:
java version "1.7.0_65"
OpenJDK Runtime Environment (IcedTea 2.5.3) (7u71-2.5.3-2)
OpenJDK 64-Bit Server VM (build 24.65-b04, mixed mode)
Oracle JRE 1.7:
java version "1.7.0_71"
Java(TM) SE Runtime Environment (build 1.7.0_71-b14)
Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode)
Oracle JRE 1.8:
java version "1.8.0_25"
Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)
The output is:
java version "1.X.X" (major, minor, often update level)
Runtime information
VM information
Optionnal things
What we want to look at here is the major JVM version ( we now support only 7 onwards).
A line like this one should do the trick:
# java -version 2>&1 | grep "java version" | sed 's%java version \"[0-9]\.\(.*\)\.[0-9].*\"%\1%'
7
- Get the version / grep it
- Extract the second number in the version string (the major version)
- Return it