The cart is empty

In a Linux environment like CentOS 7, it may be necessary to have multiple Java versions installed and switch between them as per project requirements. The Linux 'alternatives' system provides a flexible framework for managing different versions of the same program. In this article, we'll discuss how you can utilize the 'alternatives' system to easily switch between different installed Java versions on CentOS 7.

Prerequisites

Before getting started, ensure you have superuser (root) privileges on your CentOS 7 system, or you can use the sudo command to run commands as root.

Step 1: Installing Multiple Java Versions

The first step is to install the different Java versions you want to use. You can download them from the official Oracle or OpenJDK websites. For this example, we'll use OpenJDK.

  1. Update the system:

    sudo yum update
    
  2. Install the desired Java versions. For example, to install OpenJDK 8 and OpenJDK 11:

    sudo yum install java-1.8.0-openjdk
    sudo yum install java-11-openjdk
    

 

Step 2: Configuring the Alternatives System

The 'alternatives' system allows you to manage different Java versions by setting symbolic links for each version.

  1. Determine the paths to the Java installations. You can do this using the command:

    sudo update-alternatives --display java
    

    The output entries will show you the paths to different Java installations.

  2. Set an alternative for each Java installation. For OpenJDK 8 and OpenJDK 11, it might look like this:

    sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/java-1.8.0-openjdk/bin/java 1
    sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/java-11-openjdk/bin/java 2
    

Step 3: Switching Between Java Versions

Now that you have alternatives set up, you can easily switch between different Java versions using the following command:

sudo update-alternatives --config java

Step 3: Switching Between Java Versions

Now that you have alternatives set up, you can easily switch between different Java versions using the following command:

sudo update-alternatives --config java

Running this command will display a list of available Java versions along with the number assigned to each. The user can choose the desired version by entering the corresponding number.

Step 4: Verifying the Current Java Version

After setting the alternative, you can verify which Java version is currently in use by using the command:

java -version

This command will display the version, vendor, and other information about the currently set Java.

 

The 'alternatives' system on CentOS 7 provides an efficient and flexible way to manage multiple Java versions on a single system. With this method, you can easily switch between different Java versions according to the requirements of your applications and projects.