“java” command does not run the JVM that has been installed

The Problem

A JDK/JRE from Oracle has been installed on a Linux machine according to the installation instructions. The “java” command does not run the JVM that has been installed. Java applications can fail to run if an unexpected Java Runtime Environment is found.

Example from a Red Hat server:

$ java -version
java version "1.6.0_17"
OpenJDK Runtime Environment (IcedTea6 1.7.4) (rhel-1.21.b17.el6-x86_64)
OpenJDK 64-Bit Server VM (build 14.0-b16, mixed mode)

Disclaimer: The directories and paths used in this article represent fictitious sample names that make up an example. Any similarity to actual code is purely coincidental and not intended in any other manner.

The Solution

A new JDK/JRE from Oracle has been installed according to the installation instructions. Example from a Red Hat server:

# rpm -ivh jdk-7u6-linux-x64.rpm

The command “java” is usually a symbolic link in /usr/bin which points (directly or indirectly) to the actual Java executable. Usually /usr/bin is one of the first entries in the environment variable PATH and therefore /usr/bin/java is usually the one that is found first if Java is launched.

For the following possible solutions, let’s pretend that you have installed the JDK in /usr/java/jdk1.7.0_06/.

Solution 1: Modify the PATH

Modify the PATH environment variable so that the new Oracle JRE/JDK can be found before any other JREs can be found.

For example:

$ export PATH=/usr/java/jdk1.7.0_06/bin:$PATH
$ java -version
java version "1.7.0_06"
Java(TM) SE Runtime Environment (build 1.7.0_06-b24)
Java HotSpot(TM) 64-Bit Server VM (build 23.2-b09, mixed mode)

Solution 2: Use an absolute path

Use the full absolute path to the newly installed Oracle JRE/JDK in order to run Java applications.

For example:

$ /usr/java/jdk1.7.0_06/bin/java -version
java version "1.7.0_06"
Java(TM) SE Runtime Environment (build 1.7.0_06-b24)
Java HotSpot(TM) 64-Bit Server VM (build 23.2-b09, mixed mode)

Solution 3: Use update-alternatives

Use the command called update-alternatives in order to tell Linux where to find the Oracle JRE/JDK that you have just installed.

Note: manually fixing symlinks such as /usr/bin/java or /etc/alternatives/java can cause the update-alternatives command to display incorrect values or even fail to start. Therefore it is recommended to use the update-alternatives command in order to install and configure the desired symlinks instead of modifying the symlinks manually.

Use “su” on RedHat, use “sudo” on Ubuntu:

$ su -
# update-alternatives --install "/usr/bin/java" "java" "/usr/java/jdk1.7.0_06/bin/java" 1
# update-alternatives --config java

There are 3 programs which provide 'java'.

  Selection    Command
-----------------------------------------------
   1           /usr/lib/jvm/jre-1.5.0-gcj/bin/java
*+ 2           /usr/lib/jvm/jre-1.6.0-openjdk.x86_64/bin/java
   3           /usr/java/jdk1.7.0_06/bin/java

Enter to keep the current selection[+], or type selection number: 3
# exit
$ java -version
java version "1.7.0_06"
Java(TM) SE Runtime Environment (build 1.7.0_06-b24)
Java HotSpot(TM) 64-Bit Server VM (build 23.2-b09, mixed mode) 

For more information please see the man page of update-alternatives.

$ man update-alternatives
Related Post