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

The Geek Diary

CONCEPTS | BASICS | HOWTO

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • Linux Services
    • VCS
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
    • Data Guard
  • DevOps
    • Docker
    • Shell Scripting
  • Interview Questions
  • 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. What’s different between /bin/false and /sbin/nologin as nologin user’s shell
  2. How to Create Yum Repository For System Packages Installation in CentOS/RHEL
  3. How to Integrate CentOS/RHEL system into an AD Domain with LDAP/Kerberos/SSSD
  4. Linux OS Service ‘cups’
  5. How to install and configure sosreport under CentOS / RHEL
  6. How to Configure Persistent Names for Tape Devices in CentOS/RHEL
  7. CentOS / RHEL 6,7 : How to configure hugepages
  8. How to disable firewalld and and switch to iptables in CentOS / RHEL 7
  9. RHEL 7 – RHCSA Notes : Create, delete, and modify local user accounts
  10. cp: omitting directory – error while copying a directory in Linux

You May Also Like

Primary Sidebar

Recent Posts

  • Failed to start LSB: Bring up/down networking – On restarting network service CentOS/RHEL (DHCP client)
  • How To Add Timestamps To dmesg Kernel Boot Log in CentOS/RHEL
  • How to disable ICMP redirects on CentOS/RHEL
  • What are Oracle Key Vault Roles
  • Archives
  • Contact Us
  • Copyright

© 2021 · The Geek Diary