How to Install and Manage Multiple Java Versions on Linux

In this tutorial, we are going to show you how to install and manage multiple Java versions on a Linux server.

Java is an object-oriented web programming language used in thousands of applications worldwide and has a huge community of developers and regular users. Java is divided into two types OpenJDK and Oracle JDK. OpenJDK is a free and open-source implementation of the Java SE Platform Edition. Oracle JDK is the closed-source version of Java developed by Oracle. This tutorial will use the latest Ubuntu 22.04 OS, but you can choose any Linux distro. Java is compatible with all Linux distros.

Installing multiple versions of Java and switching between them is a straightforward process. Let’s get things done!

Prerequisites

  • A server with Ubuntu 22.04 as OS
  • User privileges: root or non-root user with sudo privileges

Step 1. Update the System

Before we start with the installation of Java, we must update the system packages to their latest versions.

sudo apt-get update -y && sudo apt-get upgrade -y

Step 2. Install OpenJDK Java 17

The Java OpenJDK version 17 is, by default, included in the Ubuntu 22.04 repo. To install it you can execute the following command:

sudo apt-get install openjdk-17-jdk openjdk-17-jre

Once installed, execute the following command to check the Java version:

java -version

You will receive the following output:

root@host:~# java -version
openjdk version "17.0.5" 2022-10-18
OpenJDK Runtime Environment (build 17.0.5+8-Ubuntu-2ubuntu122.04)
OpenJDK 64-Bit Server VM (build 17.0.5+8-Ubuntu-2ubuntu122.04, mixed mode, sharing)

Step 3. Install OpenJDK Java 18

The Java OpenJDK version 18 is already included in the Ubuntu 22.04 repo. So we install it, we need to execute the following command:

sudo apt-get install -y openjdk-18-jdk

Once installed, execute the following command to check the Java version:

java -version

You will receive the following output:

root@host:~# java -version
openjdk version "18.0.2-ea" 2022-07-19
OpenJDK Runtime Environment (build 18.0.2-ea+9-Ubuntu-222.04)
OpenJDK 64-Bit Server VM (build 18.0.2-ea+9-Ubuntu-222.04, mixed mode, sharing)

Step 4. Install Oracle JDK Java 17

We will install the second type of Java, Oracle version 17. To do that, first install some dependencies:

sudo apt-get install gnupg2 software-properties-common</pre

Next, add the Java repo with the following command:

add-apt-repository ppa:linuxuprising/java

Once the repo is added to install Java Oracle 17, execute the following command:

sudo apt-get install oracle-java17-installer -y

Now, the Java Oracle version is installed, and you can check it with the java -version command:

root@host:~# java -version
java version "17.0.1" 2021-10-19 LTS
Java(TM) SE Runtime Environment (build 17.0.1+12-LTS-39)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.1+12-LTS-39, mixed mode, sharing)

Step 5. Switch between different Java versions

Currently, there are three different versions installed on our server. To easily manage and switch between them, you need to execute the following command:

update-alternatives --config java

You will get the following output:

root@host:~# update-alternatives --config java
There are 3 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                         Priority   Status
------------------------------------------------------------
  0            /usr/lib/jvm/java-18-openjdk-amd64/bin/java   1811      auto mode
  1            /usr/lib/jvm/java-17-openjdk-amd64/bin/java   1711      manual mode
  2            /usr/lib/jvm/java-17-oracle/bin/java          1091      manual mode
* 3            /usr/lib/jvm/java-18-openjdk-amd64/bin/java   1811      manual mode

Press  to keep the current choice[*], or type selection number:

You just need to type the number of the listed Java version and hit Enter. Let’s type 1 so we can use the Java OpenJDK 17

Press  to keep the current choice[*], or type selection number: <strong>1</strong>
update-alternatives: using /usr/lib/jvm/java-17-openjdk-amd64/bin/java to provide /usr/bin/java (java) in manual mode

Now, you are using Java OpenJDK 17 as the default java on the system. You can check this with the following command:

java -version
The output will be the same as in Step 2. in this tutorial:
root@host:~# java -version
openjdk version "17.0.5" 2022-10-18
OpenJDK Runtime Environment (build 17.0.5+8-Ubuntu-2ubuntu122.04)
OpenJDK 64-Bit Server VM (build 17.0.5+8-Ubuntu-2ubuntu122.04, mixed mode, sharing)

You can execute again the command update-alternatives –config java, and you will see the active Jave version:

root@host:~# update-alternatives --config java
There are 3 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                         Priority   Status
------------------------------------------------------------
  0            /usr/lib/jvm/java-18-openjdk-amd64/bin/java   1811      auto mode
<strong>* 1            /usr/lib/jvm/java-17-openjdk-amd64/bin/java   1711      manual mode</strong>
  2            /usr/lib/jvm/java-17-oracle/bin/java          1091      manual mode
  3            /usr/lib/jvm/java-18-openjdk-amd64/bin/java   1811      manual mode

Press  to keep the current choice[*], or type selection number:

Our aim with today’s article is to simplify the process of installing and managing multiple Java versions for you.

Now it’s your turn to share your thoughts.

Is there something you believe we missed or any steps that have left you puzzled and needing more information? What other topics or how-to guides would you like us to provide?

Your opinion matters to us, so don’t hesitate to leave a comment below.

Leave a Comment