• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

How to work with multiple java versions under Linux

by admin

The Problem

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)

The Solution

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 multuiple 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 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)

Filed Under: CentOS/RHEL 6, CentOS/RHEL 7, Linux

Some more articles you might also be interested in …

  1. pulseaudio: command not found
  2. CentOS / RHEL : How to change SNMP community string
  3. Examples of using tcpdump command for network troubleshooting
  4. mke2fs Command Examples in Linux
  5. The System Continuously Displayed the Error Message from the “avahi-demon” in /var/log/messages
  6. How to recover from a corrupt RPM database (rebuilding an RPM database)
  7. autopkgtest Command Examples in Ubuntu
  8. What are Bash Exit Codes in Linux
  9. CentOS / RHEL 5,6 : How to reinstall GRUB loader from rescue mode
  10. How to remove the multipath device after unmapping the storage LUN from server

You May Also Like

Primary Sidebar

Recent Posts

  • pw-cat Command Examples in Linux
  • pvs: command not found
  • pulseaudio: command not found
  • pulseaudio Command Examples in Linux

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright