48

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)

9 Answers 9

92
  1. Redirect stderr to stdout.
  2. Get first line
  3. Filter the version number.

    java -version 2>&1 | head -n 1 | awk -F '"' '{print $2}'
    
Sign up to request clarification or add additional context in comments.

5 Comments

Less one process: `java -version 2>&1|awk -F\" '/version/ {print $2}'
@holygeek: & John: the command fails in AIX machine saying awk: can't open {print$2}. What could be the problem?
@Simon: this could be the quote escaping issue.
java -version 2>&1 | head -n 1 | awk -F '"' '{print $2}' | awk -F '.' '{print $2}' if you just want the major version number.
Heads up: if JAVA_TOOL_OPTIONS in in the environment, the version will not be on the first line, breaking this command.
10

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.

Comments

6

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.

java -version 2>&1 | grep -i version | cut -d'"' -f2 | cut -d'.' -f1-2

Comments

2

Since (at least on my linux system) the version string looks like "1.8.0_45":

 #!/bin/bash
 function checkJavaVers {
    for token in $(java -version 2>&1)
    do
        if [[ $token =~ \"([[:digit:]])\.([[:digit:]])\.(.*)\" ]]
        then
            export JAVA_MAJOR=${BASH_REMATCH[1]}
            export JAVA_MINOR=${BASH_REMATCH[2]}
            export JAVA_BUILD=${BASH_REMATCH[3]}
            return 0
        fi
    done
    return 1
}

#test
checkJavaVers || { echo "check failed" ; exit; }
echo "$JAVA_MAJOR $JAVA_MINOR $JAVA_BUILD"
~

2 Comments

Nice, but on older versions of bash, the RE should be quoted: if [[ $token =~ "\"([[:digit:]])\.([[:digit:]])\.(.*)\"" ]]
I'm not using an older version of bash, and that's not my dog.
2

You can use --version and in that case it's not required to redirect to stdout

java --version | head -1 | cut -f2 -d' '

From java help

-version      print product version to the error stream and exit
--version     print product version to the output stream and exit

1 Comment

Which operating systems and which Java versions? Does not work on Ubuntu 16.04 and Java 8.
2

Here's my variation (with thanks/acknowledgements to many answers prior to this). My goal is to produce a "Major Version" in line with JEP 223. Assumption are made; I don't know if it works across the board for all released (or to be released) versions of java.

java -version 2>&1 | fgrep -i version | cut -d'"' -f2 | sed -e 's/^1\./1\%/' -e 's/\..*//' -e 's/%/./'
Version Results
1.6.0_65 1.6
1.8.0_282 1.8
11.0.10 11
15.0.2 15

Comments

0

This way works for me.

# java -version 2>&1|awk '/version/ {gsub("\"","") ; print $NF}'

1.8.0_171

Comments

0
java -version 2>&1 | head -1 | awk -F '_|"' '{print $2}' 

Output

1.8.0

Or

java -version 2>&1 | head -1 | awk -F '_|"' '{print $2}' | cut -c 1-3

Output

1.8

Comments

-1

Getting only the "major" build #:

java -version 2>&1 | head -n 1 | awk -F'["_.]' '{print $3}'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.