Correct way to check Java version from BASH script

2019-01-08 11:50发布

问题:

How can I check whether Java is available (in the PATH or via JAVA_HOME) from a bash script and make sure the version is at least 1.5?

回答1:

Perhaps something like:

if type -p java; then
    echo found java executable in PATH
    _java=java
elif [[ -n "$JAVA_HOME" ]] && [[ -x "$JAVA_HOME/bin/java" ]];  then
    echo found java executable in JAVA_HOME     
    _java="$JAVA_HOME/bin/java"
else
    echo "no java"
fi

if [[ "$_java" ]]; then
    version=$("$_java" -version 2>&1 | awk -F '"' '/version/ {print $2}')
    echo version "$version"
    if [[ "$version" > "1.5" ]]; then
        echo version is more than 1.5
    else         
        echo version is less than 1.5
    fi
fi


回答2:

You can obtain java version via:

JAVA_VER=$(java -version 2>&1 | sed -n ';s/.* version "\(.*\)\.\(.*\)\..*"/\1\2/p;')

it will give you 16 for java like 1.6.0_13 and 15 for version like 1.5.0_17.

So you can easily compare it in shell:

[ "$JAVA_VER" -ge 15 ] && echo "ok, java is 1.5 or newer" || echo "it's too old..."

UPDATE: This code should work fine with openjdk and JAVA_TOOL_OPTIONS as mentioned in comments.



回答3:

You can issue java -version and read & parse the output

java -version 2>&1 >/dev/null | grep 'java version' | awk '{print $3}'


回答4:

A combination of different answers:

JAVA_VER=$(java -version 2>&1 | grep -i version | sed 's/.*version ".*\.\(.*\)\..*"/\1/; 1q')
  • Returns 7 for Java 7 and 8 for Java 8
  • Works with OpenJDK and with Oracle JDK
  • Works even if the JAVA_TOOL_OPTIONS is set


回答5:

I wrote a bash function that should work for JDK 9 and JDK 10.

#!/bin/bash

# returns the JDK version.
# 8 for 1.8.0_nn, 9 for 9-ea etc, and "no_java" for undetected
jdk_version() {
  local result
  local java_cmd
  if [[ -n $(type -p java) ]]
  then
    java_cmd=java
  elif [[ (-n "$JAVA_HOME") && (-x "$JAVA_HOME/bin/java") ]]
  then
    java_cmd="$JAVA_HOME/bin/java"
  fi
  local IFS=$'\n'
  # remove \r for Cygwin
  local lines=$("$java_cmd" -Xms32M -Xmx32M -version 2>&1 | tr '\r' '\n')
  if [[ -z $java_cmd ]]
  then
    result=no_java
  else
    for line in $lines; do
      if [[ (-z $result) && ($line = *"version \""*) ]]
      then
        local ver=$(echo $line | sed -e 's/.*version "\(.*\)"\(.*\)/\1/; 1q')
        # on macOS, sed doesn't support '?'
        if [[ $ver = "1."* ]]
        then
          result=$(echo $ver | sed -e 's/1\.\([0-9]*\)\(.*\)/\1/; 1q')
        else
          result=$(echo $ver | sed -e 's/\([0-9]*\)\(.*\)/\1/; 1q')
        fi
      fi
    done
  fi
  echo "$result"
}

v="$(jdk_version)"
echo $v

This returns 8 for Java 8 ("1.8.0_151" etc), and 9 for Java 9 ("9-Debian" etc), which should make it easier to do the further comparison.



回答6:

The method I ended up using is:

# Work out the JAVA version we are working with:
JAVA_VER_MAJOR=""
JAVA_VER_MINOR=""
JAVA_VER_BUILD=""

# Based on: http://stackoverflow.com/a/32026447
for token in $(java -version 2>&1 | grep -i version)
do
    if [[ $token =~ \"([[:digit:]])\.([[:digit:]])\.(.*)\" ]]
    then
        JAVA_VER_MAJOR=${BASH_REMATCH[1]}
        JAVA_VER_MINOR=${BASH_REMATCH[2]}
        JAVA_VER_BUILD=${BASH_REMATCH[3]}
        break
    fi
done

It will work correctly even if JAVA_TOOL_OPTIONS is set to something due to filtering done by grep.



回答7:

I know this is a very old thread but this will still be of help to others.

To know whether you're running Java 5, 6 or 7, firstly type java -version.

There will be a line in your output that looks similar to this: java version "1.7.0_55"

Then you use this table for converting the 'jargon' result to the version number.

1.7.0_55 is Java 7
1.6.0_75 is Java 6
1.5.0_65 is Java 5

Information taken from a page on the Oracle site
http://www.oracle.com/technetwork/java/javase/7u55-relnotes-2177812.html



回答8:

Using bashj, an extended bash version (https://sourceforge.net/projects/bashj/), you get a rather compact solution:

#!/usr/bin/bashj
echo System.getProperty("java.runtime.version")

The answer is provided by an integrated JVM (in ~0.017'' execution time for the script , and ~0.004'' for the call itself).



回答9:

I had a similar problem, I wanted to check which version of java was installed to perform two types of application launches. The following code has solved my problem

jdk_version() {
  local result
  local java_cmd
  if [[ -n $(type -p java) ]]
  then
    java_cmd=java
  elif [[ (-n "$JAVA_HOME") && (-x "$JAVA_HOME/bin/java") ]]
  then
    java_cmd="$JAVA_HOME/bin/java"
  fi
  local IFS=$'\n'
  # remove \r for Cygwin
  local lines=$("$java_cmd" -Xms32M -Xmx32M -version 2>&1 | tr '\r' '\n')
  if [[ -z $java_cmd ]]
  then
    result=no_java
  else
    for line in $lines; do
      if [[ (-z $result) && ($line = *"version \""*) ]]
      then
        local ver=$(echo $line | sed -e 's/.*version "\(.*\)"\(.*\)/\1/; 1q')
        # on macOS, sed doesn't support '?'
        if [[ $ver = "1."* ]]
        then
          result=$(echo $ver | sed -e 's/1\.\([0-9]*\)\(.*\)/\1/; 1q')
        else
          result=$(echo $ver | sed -e 's/\([0-9]*\)\(.*\)/\1/; 1q')
        fi
      fi
    done
  fi
  echo "$result"
}

_java="$(jdk_version)"
echo $_java

if [[ "$_java" > "$8" ]]; then
       echo version is more than 8

else
  echo version is less than 8

fi

source

I hope to be proved helpful