Home » Install JDK Ubuntu

Install JDK Ubuntu

by Online Tutorials Library

Install JDK Ubuntu

JVM (Virtual Machine of Java) and Java are needed for various types of software including Jenkins, Cassandra, Glassfish, Jetty, and Tomcat.

In this article, we will install many releases of Java Development Kit (JDK) and Java Runtime Environment (JRE) using ‘apt’. We will install OpenJDK and the official JDK as well from Oracle. We will then choose the release we wish to use for our projects. When we are finished, we will be able to apply the JDK for developing software or using the Java Runtime for running software.

The repositories of Ubuntu 20.04 (default) contain two packages of OpenJDK, Java Development Kit, and Java Runtime Environment. The Java Runtime Environment is composed of the Java Virtual Machine, binaries, and classes that permit us to execute Java programs. The Java Development Kit contains the Java Runtime Environment and debugging/development tools as well as libraries mandatory for building Java applications.

Prerequisites

We will need to follow this article:

The 20.04 server set up of Ubuntu by following the 20.04 initial server setup guide of Ubuntu including a non-root sudo user and a firewall.

Install the default JDK/JRE

One of the easiest options to install Java is to apply the version which is packaged using Ubuntu. The 20.04 version of Ubuntu by default includes the 11 version of OpenJDK and it is an open-source version of the JDK and JRE.

First, we need to update the index of the package for installing this version.

Install JDK Ubuntu

Next, see if the Java version is installed already.

Install JDK Ubuntu

We will see the following result if the Java version is not installed currently.

We need to run the below command for installing the default Java Runtime Environment which will install the JRE using OpenJDK 11.

Install JDK Ubuntu

The Java Runtime Environment will permit us to execute almost every Java software.

We can verify our installation using the following command.

We will find results similar to the below:

Install JDK Ubuntu

We may require the Java Development Kit in addition to the Java Runtime Environment for compiling and running some particular Java-based software. We need to run the below command for installing the JDK which will install the JRE as well.

Install JDK Ubuntu

We need to verify the JDK is successfully installed by checking the Java Compiler javac version.

Install JDK Ubuntu

We will see e the below result:

Next, let’s see how to install the official JDK and JRE of Oracle.

Install Oracle JDK 11

The licensing agreement of Oracle for Java does not permit automatic installation from the package manager. We should create an account of Oracle and download the JDK manually for adding any new package repository for the release we had like to use for installing the Oracle Java Development Kit which is the official release shared by Oracle. Then, we can apply apt for installing it with the help of the third-party installation script.

We will need to install must match the release of the installer script of the JDK version of Oracle. We should visit a page, i.e., ‘oracle-java11-installer’ for finding out which release we need.

The script version is 11.0.7 on this page. We would require the 11.0.7 version of Oracle JDK in this case. Our version number might vary depending on when we are installing the software. We do not need to install anything using this page, we will download the script of the installation from apt shortly.

Then, we need to check the downloads page and after that locate the release that matches the one we need.

  • Click on the button, i.e., ‘JDK Download’ and we will be jumped to a screen that displays the releases available.
  • Click on the package, i.e., ‘.tar.gz’ for Linux.
    We will be illustrated with the screen prompting us for accepting the license agreement of Oracle.
  • Choose the checkbox for accepting this license agreement and click on the Download button.
  • Our download will start.
  • We might require to login into our Oracle account multiple times before the download begins.
  • We will need to send it to our server when the file has been downloaded. We will upload this file to our server on our local machine.
  • On Windows, Linux, or, macOS with the help of Windows Subsystem for Linux, we will apply the command, i.e., ‘scp’ for transferring the file to our home directory of Sammy user.
  • The below command assumes that we have saved the JDK file of Oracle to the Downloads folder of our local machine.

We will return to our server and add a repository of the third party that will support us to install the Java of Oracle when the file uploading process has been completed. We will install a package, i.e., ‘software-properties-common’ which will add a command, i.e., ‘add-apt-repository’ to our system.

Install JDK Ubuntu

Next, we will import a signing key which is used for verifying the software we are about to install:

Install JDK Ubuntu

We will see the following output:

Then, we will use a command, i.e., ‘add-apt-repository’ for adding the repository to our list of the package sources:

Install JDK Ubuntu

We will see the following message:

We need to press the enter button for continuing the installation process. We might find a message about ‘no valid OpenPGP data found’, but we can ignore this message safely.

We will update our package list for making the new software present for the installation process using the following command:

Install JDK Ubuntu

The installer would see for the Oracle JDK we downloaded in the /var/cache/oracle-jdk11-installer-local directory. We will make this directory and transfer the archive of Oracle JDK there.

Install JDK Ubuntu

Finally, we will install our package using the following command:

Install JDK Ubuntu

First, the installer will ask us for accepting the license agreement of Oracle. We will accept the agreement and the installer would extract the package of Java and install it.

Now, we will see how to choose which Java version we wish to use.

Java Management

We can have more than one installation of Java on a single server. We can configure which release is the default for apply on the terminal with the help of a command, i.e., ‘update-alternatives’. The command is mentioned below:

Install JDK Ubuntu

It is what the result will look like if we have installed both releases of Java in this article.

Select the number corresponding to the version of Java to apply it as a default or click on the Enter button for leaving the current settings in position.

We can do it for other commands of Java like the javac compiler. The command is mentioned below:

Many other commands to which this command could be executed include, but aren’t restricted to: keytool, jarsigner, and javadoc.

JAVA_HOME Environment Variable Setting

Several programs written in the Java language use the environment variable, i.e., JAVA_HOME for determining the installation location of Java.

First, we need to determine that where the Java version is installed for setting the environment variable.

We will use the following command:

Install JDK Ubuntu

This command will display all installations of Java with the installation path.

In this type of case, the paths of the installation are below:

  1. OpenJDK 11 is positioned at the /usr/lib/jvm/java-11-openjdk-amd64/bin/java directory.
  2. Oracle Java is positioned at the /usr/lib/jvm/java-11-oracle/jre/bin/java directory.

We need to copy the path using our preferred installation. After that, we will open the /etc/environment directory with the help of the nano text editor or any other text editors.

We will add the below line at the completion of this file. Ensure for replacing the highlighted path with our copied path, but don’t add the path portion of the bin/.

We will modify this file which will set the path, i.e., JAVA_HOME for every user on our system.

We will save this file and exit from the text editor.

Now, we will reload the file for applying the modifications to our current session:

We will verify that this environment variable has been set with the help of the following command:

We will find the path we just set.

Many other users will require to run the source /etc/environment command or log back and log out to use this setting.


Next Topic#

You may also like