Checking if java is installed in linux

Correct way to check Java version from BASH script

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?

Java 5 has been end-of-life for quite a long time. Java 6 will be end-of-life very soon. You should be moving to Java 6 as a priority, and to 7 as soon as you can.

@kittylyst: This is a minimal requirement. My software doesn’t need Java 6 features, so there is no point forcing users for something better. But thanks for pointing it out anyway.

17 Answers 17

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/ ') 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 

Ug sorry cant get comments to format. This fails when Java version is 1.10. This is because string comapre is not a number compare. Also the answer is wrong when version is «1.5» Though I guess that would be «1.5.0» so I guess its okay.

good point. you could do something like: IFS=. read major minor extra 5 )); . — would have to check for invalid octal numbers like «08».

the » type -p java » won’t work in «sh»; so, for portability, use #!/bin/bash in the script, rather than #!/bin/sh (though, it’s possible that /bin/sh isn’t actually bourne shell, but rather a backward compatible variant)

For comparing versions, I suggest : version1=$(echo «$version» | awk -F. ») which will get you major and minor aligned with 3 digit for each, and then compare with if [ $version1 -ge 001008 ] ; then

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 , 15 for version like 1.5.0_17 and 110 for openjdk 11.0.6 2020-01-14 LTS .

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.

The result of ‘java -version’ with OpenJDK 8 is slightly different, «java» is replaced by «openjdk» in the output. This expression works with both cases: sed ‘s/.*version «\(.*\)\.\(.*\)\..*»/\1\2/; 1q’

If JAVA_TOOL_OPTIONS is set the output is again slightly different, the java version is not on the first line. eg: Picked up JAVA_TOOL_OPTIONS: -agentlib:hprof java version «1.7.0_60» Java(TM) SE Runtime Environment (build 1.7.0_60-b19) Java HotSpot(TM) 64-Bit Server VM (build 24.60-b09, mixed mode) Dumping Java heap . allocation sites . done

Читайте также:  Linux find biggest file

Some tweak to this as below should work after Java 9 as well. JAVA_VER=$(java -version 2>&1 | sed -n ‘;s/.* version «(.*)\.(.*)\..*»/\1\2/p;’)

The answers above work correctly only for specific Java versions (usually for the ones before Java 9 or for the ones after Java 8.

I wrote a simple one liner that will return an integer for Java versions 6 through 11 (and possibly all future versions, until they change it again!).

It basically drops the «1.» at the beginning of the version number, if it exists, and then considers only the first number before the next «.».

java -version 2>&1 | head -1 | cut -d'»‘ -f2 | sed ‘/^1\./s///’ | cut -d’.’ -f1

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

java -version 2>&1 >/dev/null | grep 'java version' | awk '' 

This is very close to what I want. Do you know of a version which also searches $PATH before it calls locate (which isn’t always available)?

@Ravi, right I will edit those links, you could use java -version 2>&1 >/dev/null | grep ‘java version’ | awk ‘‘ by the time

If interested in the numerical part only and for further usage in path variables, you may use something like java -version 2>&1 | head -1 | cut -d ‘»‘ -f 2

You could also compare the jdk class version number with javap :

javap -verbose java.lang.String | grep "major version" | cut -d " " -f5 

If its java 8, this will print 52, and 55 for java 11. With this you can make a simple comparison and you don’t have to deal with the version string parsing.

This is a nice hack if you just want to know the major version. Sometimes, you’d like to check that a certain patchlevel is installed.

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\.\(8*\)\(.*\)/\1/; 1q') else result=$(echo $ver | sed -e 's/\(3*\)\(.*\)/\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.

Читайте также:  Linux проверка прав доступа

Determining the Java version only using grep with Perl-style regex can be done like this:

java -version 2>&1 | grep -oP 'version "?(1\.)?\K\d+' 

This will print the major version for Java 9 or higher and the minor version for versions lower than 9, for example 8 for Java 1.8 and 11 for Java 11.0.4 .

JAVA_MAJOR_VERSION=$(java -version 2>&1 | grep -oP 'version "?(1\.)?\K\d+' || true) if [[ $JAVA_MAJOR_VERSION -lt 8 ]]; then echo "Java 8 or higher is required!" exit 1 fi 

The || true was added to prevent the script from aborting in case Java is not installed and the Shell option set -e was enabled.

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
java -version |& grep 'version' |& awk -F\" '< split($2,a,"."); print a[1]"."a[2]>' 
#!/bin/bash echo `java -version 2>&1 | grep 'version' 2>&1 | awk -F\" '< split($2,a,"."); print a[1]"."a[2]>'` jver=`java -version 2>&1 | grep 'version' 2>&1 | awk -F\" '< split($2,a,"."); print a[1]"."a[2]>'` if [[ $jver == "1.8" ]]; then echo $jver is java 8 else echo $jver is java 11 fi 

Small improvements: Use $(. ) instead of backticks. They are safer, support nesting, and easier to type on most keyboards/OSs.

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=$ JAVA_VER_MINOR=$ JAVA_VER_BUILD=$ break fi done 

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

Another way is: A file called release is located in $JAVA_HOME . On Java 15 (Debian) it has as content:

IMPLEMENTOR="Debian" JAVA_VERSION="15.0.1" JAVA_VERSION_DATE="2020-10-20" MODULES="java.base java.compiler java.datatransfer java.xml java.prefs java.desktop java.instrument java.logging java.management java.security.sasl java.naming java.rmi java.management.rmi java.net.http java.scripting java.security.jgss java.transaction.xa java.sql java.sql.rowset java.xml.crypto java.se java.smartcardio jdk.accessibility jdk.internal.vm.ci jdk.management jdk.unsupported jdk.internal.vm.compiler jdk.aot jdk.internal.jvmstat jdk.attach jdk.charsets jdk.compiler jdk.crypto.ec jdk.crypto.cryptoki jdk.dynalink jdk.internal.ed jdk.editpad jdk.hotspot.agent jdk.httpserver jdk.incubator.foreign jdk.internal.opt jdk.jdeps jdk.jlink jdk.incubator.jpackage jdk.internal.le jdk.internal.vm.compiler.management jdk.jartool jdk.javadoc jdk.jcmd jdk.management.agent jdk.jconsole jdk.jdwp.agent jdk.jdi jdk.jfr jdk.jshell jdk.jsobject jdk.jstatd jdk.localedata jdk.management.jfr jdk.naming.dns jdk.naming.rmi jdk.net jdk.nio.mapmode jdk.sctp jdk.security.auth jdk.security.jgss jdk.unsupported.desktop jdk.xml.dom jdk.zipfs" OS_ARCH="x86_64" OS_NAME="Linux" SOURCE="" 

So you see JAVA_VERSION , which contains the version number.

major=$(echo $JAVA_VERSION | cut -d. -f1) 

sets major to the value 15 (=Major version number), which you can use further.

It’s a really simple solution.

Источник

How to Check Java Version Installed on Linux

How do I check my current Java version? There are several ways to check if Java is installed and which version is running on your system.

In this tutorial, learn how to check the Java version installed on Linux distros, including Ubuntu, CentOS, and Debian.

Читайте также:  Установка плагина госуслуги linux

tutorial on checking Java version on linux

  • A user account with sudo privileges
  • Access to the command-line/terminal window
  • A version of Java

Method 1: Check the Java Version On Linux

To check the Java version on Linux Ubuntu/Debian/CentOS:

2. Run the following command:

3. The output should display the version of the Java package installed on your system. In the example below, OpenJDK version 11 is installed.

example of checking the version of java running on ubuntu linux

Note: If the output indicates there is no such package on the system, you can install it with the help of one of our guides – How to install Java on Ubuntu or How to Install Java on CentOS.

You can also check the version of the primary Java compiler – javac (pronounced “java-see”) with the command:

Check javac version on Ubuntu.

Method 2: Find Version by Checking Path Where Java is Installed

There are two ways to find the path of the Java directory.

The first option includes running a single command:

update-alternatives --list java

The system should respond with the path where Java is installed.

Check Java directory path to find the java installation version

Note: This option may not work on CentOS systems. If you have issues finding the path of the Java directory with the command above, use the alternative outlined below.

Alternatively, you can use the whereis command and follow the symbolic links to find the Java path.

Check Java path on Ubuntu.

The output tells you that Java is located in /usr/bin/java.

2. List the content of the /usr/bin/java directory:

Locate Java directory on Ubuntu.

Inspecting the directory shows that /usr/bin/java is only a symbolic link for /etc/alternatives/java.

3. Just like in the previous step, list the content of the provided path by running:

Find Java path on Ubuntu.

Finally, the output displays /etc/alternatives/java is another symbolic link and that the real path of the Java directory is /usr/lib/jvm/java-11-openjdk-amd64/bin/java.

Method 3: Search for Java in the Installed Packages List

You can also prompt the system to list installed packages and search for Java, with its version number.

Find Java by listing all installed packages.

1. To generate a list of all installed packages, use the command:

2. Scroll up/down until you find the Java packages as shown in this example.

Find Java in the installed packages list.

To avoid searching through all installed packages, list Java packages only. Prompt the system to list a specific software package. In this case, the package name is openjdk:

sudo apt list --installed | grep -i openjdk

Search for Java in the Installed packages list on Ubuntu.

Note: CentOS users need to modify the commands for listing installed packages for their package manager. Use the commands: sudo yum list installed and sudo yum list installed | grep -i openjdk instead.

With this article, you have successfully checked the Java version installed on Linux. We also covered checking the Java path and searching for Java among the installed packages.

Once the Java version is confirmed, you can start developing anything from lightweight mobile to desktop applications.

Источник

Оцените статью
Adblock
detector