How to work with multiple java versions under Linux?

When i installed a new version of the Java Runtime Environment but the “java -version” command does not show the new version.

# java -version
java version "1.7.0_65"
OpenJDK Runtime Environment (rhel-2.5.1.2.el6_5-x86_64 u65-b17)
OpenJDK 64-Bit Server VM (build 24.65-b04, mixed mode)

 

This problem could happen due to multiple installs of java on the system. If multiple versions of OpenJDK packages are installed on the system, alternatives command maybe be used to set the default version of java. From the man pages of “alternatives” command:

alternatives - maintain symbolic links determining default commands

 

To set a default version of java when you have multiple java versions installed, use the below command.

# alternatives --config java

There are 2 programs which provide 'java'.

  Selection    Command
-----------------------------------------------
*+ 1           /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java
   2           /usr/lib/jvm/jre-1.6.0-openjdk.x86_64/bin/java

Enter to keep the current selection[+], or type selection number: 2

 

How to install new java path to the alternatives java group?

 

You can also manually install a new Java path to the alternatives java group. This can be done using the update-alternatives command. The syntax to do it is as follows.

# update-alternatives --install [link] [name] [path] [priority]

 

You can then set the newly added java path as system default using the “update-alternatives” command.

# update-alternatives --set [name] [path]

OR

# alternatives --set [name] [path]

 

1. For example, in case of jre-1.7.0-openjdk.x86_64, below command can be used to install it and set as the default java version to be used.

# update-alternatives --install "/usr/bin/java" "java" /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java 99999
# update-alternatives --set java /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java 

Here, 99999 – is the priority (It should be the highest for the java version to be the default one.) 

 

2. Using alternatives, check if changes have taken effect and finally confirm the java version on the system.

# alternatives --config java

There are 2 programs which provide 'java'.

  Selection    Command
-----------------------------------------------
*+ 1           /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java    ### default java version
   2           /usr/lib/jvm/jre-1.6.0-openjdk.x86_64/bin/java

Enter to keep the current selection[+], or type selection number: 

 

3. You can also use the “java -version” command to check the current version of java.

# java -version
java version "1.7.0_65"
OpenJDK Runtime Environment (rhel-2.5.1.2.el6_5-x86_64 u65-b17)
OpenJDK 64-Bit Server VM (build 24.65-b04, mixed mode) 

 

Was this answer helpful? 0 Users Found This Useful (0 Votes)