SoFunction
Updated on 2025-05-08

Detailed steps for multi-version JDK installation and configuration of CentOS

1. Download JDK

You can download different versions of JDK installation packages from the official Oracle website or related sources of OpenJDK. Take JDK 8 and JDK 21 as examples, assuming you have downloaded them locally.

2. Install JDK

Create a directory for the JDK and unzip the downloaded JDK installation package to that directory.

# Create a directorysudo mkdir -p /usr/local/java
# Unzip JDK 8sudo tar -zxvf  -C /usr/local/java
# Unzip JDK 21sudo tar -zxvf jdk-21_linux-x64_bin. -C /usr/local/java

3. Configure environment variables

The following are two commonly used methods: install and manage multiple JDK versions on CentOS 10 systems. One is to use the update-alternatives tool to switch globally, and the other is to switch temporarily by setting environment variables and alias. The following detailed steps are provided:

Method 1: Use update-alternatives to manage globally

This method can set the default JDK at the system level. Each JDK registers a priority and then switches the default version via commands.

  • Register each JDK
sudo update-alternatives --install /usr/bin/java java /usr/java/jdk1.8.0_xxx/bin/java 1
sudo update-alternatives --install /usr/bin/java java /usr/java/jdk-11.0_xxx/bin/java 2
sudo update-alternatives --install /usr/bin/java java /usr/java/jdk-17.0_xxx/bin/java 3

sudo update-alternatives --install /usr/bin/javac javac /usr/java/jdk1.8.0_xxx/bin/javac 1
sudo update-alternatives --install /usr/bin/javac javac /usr/java/jdk-11.0_xxx/bin/javac 2
sudo update-alternatives --install /usr/bin/javac javac /usr/java/jdk-17.0_xxx/bin/javac 3

Please replace the "xxx" part with the specific version number path.

  • Switch the default JDK

    sudo update-alternatives --config java
    sudo update-alternatives --config javac
    

    The system will list each registered version and enter the corresponding number to switch.

  • Verified version

    Run the following command to confirm the current default version:

    java -version
    javac -version
    

Method 2: Switch through environment variables and alias (user level)

This method is suitable for situations where a temporary switch to the JDK version or only effective for the current user.

  • Edit environment variable configuration fileRevise/etc/profile(Global) or~/.bash_profile(Current user) file, add the following content (three alias commands are configured in the example):

    After editing is complete, remember to save and exit (for example, press   in vimEscAfter input:wqSave and exit; press   in nanoCtrl+OSave, thenCtrl+Xquit).

    sudo vi /etc/profile
    sudo nano /etc/profile
    
    vim ~/.bash_profile
    nano ~/.bash_profile
    
    ## Default pointing to JDK 8export JAVA_HOME=/usr/local/java/jdk1.8.0_202
    export PATH=$JAVA_HOME/bin:$PATH
    
    alias java8='export JAVA_HOME=/usr/local/java/jdk1.8.0_202 && export PATH=$JAVA_HOME/bin:$PATH && java -version'
    alias java21='export JAVA_HOME=/usr/local/java/jdk-21.0.6 && export PATH=$JAVA_HOME/bin:$PATH && java -version'
    ## alias java17='export JAVA_HOME=/usr/local/java/jdk-17.0_xxx && export PATH=$JAVA_HOME/bin:$PATH && java -version'
    

    After saving the file, let the configuration take effect:

    source /etc/profile   
    # orsource ~/.bash_profile
    
  • Switch the JDK version

    When you need to switch to a certain version, just execute the corresponding alias command, for example:

    java21
    
  • Verify the switching effectrun:

    java -version
    

    to confirm that the set JDK is currently being used.

  • Persistent configuration: The current configuration only takes effect in the current terminal session. If you want to use these configurations every time you open the terminal, you need to add them to the shell's configuration file, like~/.bashrc(For Bash shell) or~/.zshrc(For Zsh shell). Examples of commands to add configuration are as follows:

    echo 'export JAVA_HOME=/usr/local/java/jdk1.8.0_202' >> ~/.bashrc
    echo 'export PATH=$JAVA_HOME/bin:$PATH' >> ~/.bashrc
    echo 'alias java8='export JAVA_HOME=/usr/local/java/jdk1.8.0_202 && export PATH=$JAVA_HOME/bin:$PATH && java -version'' >> ~/.bashrc
    echo 'alias java21='export JAVA_HOME=/usr/local/java/jdk-21.0.6 && export PATH=$JAVA_HOME/bin:$PATH && java -version'' >> ~/.bashrc
    

    After the addition is complete, execute the following command to make the configuration take effect:

    source ~/.bashrc
    
    • Missing error handling: The current configuration does not perform error handling for cases where the directory does not exist or the JDK version is unavailable. You canaliasAdd some error checking logic to the command, the examples are as follows:
    alias java8='if [ -d "/usr/local/java/jdk1.8.0_202" ]; then export JAVA_HOME=/usr/local/java/jdk1.8.0_202 && export PATH=$JAVA_HOME/bin:$PATH && java -version; else echo "The JDK 8 directory does not exist"; fi'
    alias java21='if [ -d "/usr/local/java/jdk-21.0.6" ]; then export JAVA_HOME=/usr
    
  • update-alternativesThe method is suitable for system-level JDK management and is suitable for scenarios where everything needs to be replaced with the default JDK between different projects.

  • Environment variables and aliasThe method is suitable for users to switch temporarily, or is flexibly set for different needs (such as development and testing).

Summarize

This is the end of this article about CentOS installation and configuration of multi-version JDK. For more related content on CentOS installation and configuration of multi-version JDK, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!