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)
First, click on the magnifying glass and type “cmd”, then click on the Command Line app icon that appears. Now, enter the command java -version and you'll see the version of Java listed.
/opt/java-selection/openjdk-java-8. bash : This script changes your Java version to OpenJDK Java 8 in your current terminal/shell, and all future terminals and shells.
Filter the version number.
java -version 2>&1 | head -n 1 | awk -F '"' '{print $2}'
This is a slight variation, but PJW's solution didn't quite work for me:
java -version 2>&1 | head -n 1 | cut -d'"' -f2
just cut the string on the delimiter "
(double quotes) and get the second field.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With