Pentarock Technologies

SHARING KNOWLEDGE

How to Install Multiple Versions of Java on Ubuntu 20.04 LTS

7 min read
How to Install Multiple Versions of Java on Ubuntu 20.04 LTS

How to Install Multiple Versions of Java on Ubuntu 20.04 LTS – Introduction

There are number of applications that use Java Virtual Machine (JVM) and Java to run. Java is one of the most popular programming languages. It is a platform independent programming language.  Here, in this tutorial we will learn how to install multiple versions of Java on Ubuntu 20.04 LTS. In this tutorial we will install Java Development Kit using the apt command of Ubuntu 20.04 LTS. We will also learn how to install different versions of JDK on Ubuntu 20.04.

First we will install OpenJDK using the apt command. Then we will also install official JDK from the Oracle website. We will learn how to select a specific version of JDK for our different projects.  Java Runtime Environment will be installed automatically along with JDK installation.

The objective of this tutorial is to enable our readers to be able to install and use different versions of JDK and JVM on Ubuntu 20.04 LTS operating system.

Prerequisites for Installing JVM and JDK on Ubuntu 20.04 LTS

Following are the prerequisites for installing JVM and JDK on Ubuntu 20.04 LTS.

  1. Firstly we will require a Desktop or a Laptop system with preinstalled Ubuntu 20.04 LTS Desktop or Server version of operating system.

If you want to install Ubuntu 20.04 LTS on a new system then you can follow our tutorial ‘How to Install Ubuntu 20.04 LTS Focal Fossa’. The link to this tutorial is given below:

How to Install Ubuntu 20.04 LTS Focal Fossa
  1. Next we will require root user permissions or non root sudo permissions to download and install the software.
  2. We will also require a stable and fast internet connection for downloading and installing the software from the official website.

Installation of Default Version of Java

First we will see how to install the default version of Java on Ubuntu 20.04. Ubuntu operating system always has a default version of Java in its repository. Installing the default version of Java is the simplest method of installing Java. The default version of Java repository contains the open-source version of Java that is, OpenJDK and JRE, the development kit and the runtime environment. Now to install the default version of Java, open the terminal window and issue the following commands in the given sequence.

sudo apt update

Furthermore, before installing the Java, we should verify whether it is pre-installed on our system or not. To verify this, issue the following command in the terminal window.

java -version

If Java is not installed on the system then the following will be the output of the above command.

java -version
Command ‘java’ not found, but can be installed with:

apt install openjdk-11-jre-headless  # version 11.0.11+9-0ubuntu2~20.04, or

apt install default-jre              # version 2:1.11-72

apt install openjdk-13-jre-headless  # version 13.0.7+5-0ubuntu1~20.04

apt install openjdk-16-jre-headless  # version 16.0.1+9-1~20.04

apt install openjdk-8-jre-headless   # version 8u292-b10-0ubuntu1~20.04

However, to install the default version of the Java Runtime environment, open the terminal window and issue the following command.

sudo apt install default-jre

You will be prompted to accept the installation by the apt installer. On entering ‘Y’ or yes the apt installer will install the default JRE on your system. To verify the installation of Default-JRE, issue the following command in the terminal window.

java -version

The output of the above command will confirm the installation of Default-JRE version by showing the installed version on the screen as shown below.

java -version
openjdk version “11.0.11” 2021-04-20

OpenJDK Runtime Environment (build 11.0.11+9-Ubuntu-0ubuntu2.20.04)

OpenJDK 64-Bit Server VM (build 11.0.11+9-Ubuntu-0ubuntu2.20.04, mixed mode, sharing)

Additionally, for the purpose of Java Programming we will need to compile and run the Java code on our system. To compile and execute the Java code on our system we will require Java Development Kit (JDK) to be installed on our system. To install the Default JDK version on Ubuntu 20.04 LTS, issue the following command in the terminal windows as given below.

sudo apt install default-jdk

Version Check

The above command will prompt you to provide the permission to install JDK on your system. When the confirmation is provided in affirmative, then the apt installer will install the JDK software on the system. After the installation of the OpenJDK is complete, then we should verify whether the JDK is correctly installed on our system. To verify the installation issue the following command in the terminal window.

javac -version

If the open source and default Java development Kit is correctly installed, we will see the following output on the screen.

 javac -version
javac 11.0.11

That’s it. Now we have successfully installed the default JRE and JDK on our Ubuntu 20.04 LTS system.

Install Java 8 on Ubuntu 20.04 LTS Focal Fossa

Java 8 is an old version of Java. It was one of the most popular versions of Java. Also Java 8 is a long time support version. If some of our applications are developed in Java 8. If you want to install Java 8 on your system then open the terminal windows and issue the following command as given below.

sudo apt install openjdk-8-jdk

The apt installer will prompt for confirmation and on the affirmative response the installation of the Java 8 software will start. The apt installer will automatically install Java 8 on your system along with all of its dependencies. After the

Now if we have more than one versions of Java installed on our system then we have to set the default Java version as per our requirements. To set the default version of Java we use the update alternatives command. As in our case we have two versions of Java installed on our system one is Java 8 the older version and the other one is the latest version 11. Now to set the older version 8 of Java we will execute the following command in the terminal window.

sudo update-alternatives --config java

The output of the above command will be as shown in the figure below. 

sudo update-alternatives –config java
There are 2 choices for the alternative java (providing /usr/bin/java).  Selection    Path                                            Priority   Status

————————————————————

* 0            /usr/lib/jvm/java-11-openjdk-amd64/bin/java      1111      auto mode 

1            /usr/lib/jvm/java-11-openjdk-amd64/bin/java      1111      manual mode 

2            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081      manual mode

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

Now to select the default version of Java to java 8 type the number 2 and hit the enter button. This will set java 8 as the default version. Now to verify the default version issue the following command in the terminal window.

java -version

The output of the above command will be like the given below.

java -version
openjdk version “1.8.0_292”

OpenJDK Runtime Environment (build 1.8.0_292-8u292-b10-0ubuntu1~20.04-b10)

OpenJDK 64-Bit Server VM (build 25.292-b10, mixed mode)

That’s it; the installation of older version 8 of Java is now complete.

How to Install Multiple Versions of Java on Ubuntu 20.04 LTS

Installation of Oracle Java SE Development Kit 16

Now we will learn how to install Oracle Java Standard Edition Development Kit Version 16. At the time of writing this article Oracle Java Standard Edition Development Kit Version 16 is the latest version available on the official website. To install this version first we have to open the official website’s download page in our favorite browser. Then we have to select the .deb file of the installer version. The URL to the official website’s download page is given below.

The page looks like the one given below.

how to install multiple versions of java on Ubuntu 20.04 LTS

Now click on the download link shown with the arrow in the figure above. This will navigate to the next download page. Scroll down the page and you will see a download list of installer files of Java SE Development Kit for different operating systems.

Oracle Java Download Page 1Our operating system is Ubuntu 20.04 LTS therefore click on the Linux x64 Debian Package link of the installer file with the extension  .deb to download the installer file. When you will click on the download link, a popup asking to agree to the license agreement will appear on the screen as shown below.

License Agreement

Now, accept the agreement by clicking and check mark the box provided for the purpose and click download button to start the download of the installer file. Now navigate to the folder where the installer file is downloaded. In my case the installer file is download in Downloads folder.  Then issue the following command in the terminal window to begin the installation.

sudo dpkg -i jdk-16.0.2_linux-x64_bin.deb

After the completion of the above command the Oracle Java SE Development Kit 16 installation is complete and you can start using Java version of your choice.

Set JAVA_HOME Environment Variable

Here, we will set the JAVA_HOME Environment Variable. Many programs need JAVA_HOME environment variable to find the location of Java installation on the disk for their proper execution. We will set the JAVA_HOME environment variable for Installation of Oracle Java SE Development Kit 16. Now open the terminal window and run the following command set the path.

sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-16.0.2/bin/java 1
sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk-16.0.2/bin/javac 1

Now run the following command in the terminal window to check all the installed versions on java on your system.

sudo update-alternatives --config java

The output of the above command will be similar to as given below.

sudo 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-11-openjdk-amd64/bin/java      1111      auto mode

1            /usr/lib/jvm/java-11-openjdk-amd64/bin/java      1111      manual mode

2            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081      manual mode

3            /usr/lib/jvm/jdk-16.0.2/bin/java                 1         manual mode

Press <enter> to keep the current choice[*], or type selection number: 3

update-alternatives: using /usr/lib/jvm/jdk-16.0.2/bin/java to provide /usr/bin/java (java) in manual mode

Now select 3 and the default version of Java on your machine will become Oracle Java Version 16.

That is all done. The installation and the configuration of Multiple Versions of Java on Ubuntu 20.04 LTS is complete and now you can start using any of the versions of java as you may like. Enjoy!

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.