INSTALL AND MANAGE MULTIPLE JAVA JDK AND JRE VERSIONS ON UBUNTU

INSTALL AND MANAGE MULTIPLE JAVA JDK AND JRE VERSIONS ON UBUNTU

Step 1. Check Java JDK version

Open a terminal and check java JDK version:

$ javac -version

If you do not have a Java JDK installed the terminal response will look like:

Command 'javac' not found, but can be installed with:

sudo apt install default-jdk              # version 2:1.11-72, or
sudo apt install openjdk-11-jdk-headless  # version 11.0.8+10-0ubuntu1~20.04
sudo apt install openjdk-13-jdk-headless  # version 13.0.4+8-1~20.04
sudo apt install openjdk-14-jdk-headless  # version 14.0.2+12-1~20.04
sudo apt install openjdk-8-jdk-headless   # version 8u265-b01-0ubuntu2~20.04
sudo apt install ecj                      # version 3.16.0-1

As you can see, the terminal response displays the commands to install various headless JDK versions.

Step 2. Install Java JRE/JDKs

1. Install Java 11 JRE/JDK

At the time of writing, Java 11 is the latest long-term supported (LTS) version of Java. It is the default Java development and runtime version.

Installing a JDK package will also install the corresponding JRE.

Install Java 11 JDK:

Check JRE version:

The response will look like:

Check JDK version:

The response will look like:

2. Install Java 8 JRE/JDK

Install Java 8 JDK:

If you check current JDK and JRE versions it will still display Java 11.

3. Install Java 13 JRE/JDK

Install Java 13 JDK:

Check current JRE version:

The response will look like:

Check current JDK version:

The response will look like:

As you can see the current JRE and JDK are version 13.

4. Install Java 14 JRE/JDK

Install Java 14 JDK:

Check current JRE:

The response will look like:

Check current JDK:

The response will look like:

Again, we can see the current JRE and JDK are version 14.

Step 2. Manage Installed Java JRE/JDKs

Now that we have installed multiple Java JRE/JDK versions we can switch between them.

1. Switch JRE version

Check installed JREs:

The response will look like:

Now you can switch to another JRE or keep the current version.

2. Switch JDK version

Check installed JDKs:

The response will look like:

Again, you can now switch to another JDK or keep the current version.

Step 3. Add JAVA_HOME environment variable

Java applications may use environment variables. JAVA_HOME is a common one so we will now we add this.

Edit /etc/environment file:

Add the following line to the file and save:

In the above example we are using Java 11.

To apply the changes you will need to log out then log in again. To apply the changes in your current terminal session use the source command:

Check the environment variable was set:

The response should look like:

Last updated

Was this helpful?