How To Install JDK
How To Install JDK
04
Introduction
Java and the JVM (Java’s virtual machine) are required for many kinds of software,
including Tomcat, Jetty, Glassfish, Cassandra and Jenkins.
In this guide, you will install various versions of the Java Runtime Environment (JRE) and the Java
Developer Kit (JDK) using apt. You’ll install OpenJDK as well as the official JDK from Oracle. You’ll then
select the version you wish to use for your projects. When you’re finished, you’ll be able to use the JDK
to develop software or use the Java Runtime to run software.
Prerequisites
One Ubuntu 20.04 server set up by following the the Ubuntu 20.04 initial server setup
guide tutorial, including a sudo non-root user and a firewall.
$ java --version
Output
The JRE will allow you to run almost all Java software.
Verify the installation with:
$ java --version
You may need the Java Development Kit (JDK) in addition to the JRE in order to compile and run some
specific Java-based software. To install the JDK, execute the following command, which will also install
the JRE:
$ sudo apt install default-jdk
Verify that the JDK is installed by checking the version of javac, the Java compiler:
$ javac --version
Next, let’s look at how to install Oracle’s official JDK and JRE
Oracle’s licensing agreement for Java doesn’t allow automatic installation through package managers.
To install the Oracle JDK, which is the official version distributed by Oracle, you must create an Oracle
account and manually download the JDK to add a new package repository for the version you’d like to
use. Then you can use apt to install it with help from a third party installation script.
The version of Oracle’s JDK you’ll need to download must match version of the installer script. To find
out which version you need, visit the oracle-java11-installer page.
You don’t need to download anything from this page; you’ll download the installation script
through apt shortly.
Then visit the Downloads page and locate the version that matches the one you need.
Click the JDK Download button and you’ll be taken to a screen that shows the versions available. Click
the .tar.gz package for Linux.
You’ll be presented with a screen asking you to accept the Oracle license agreement. Select the
checkbox to accept the license agreement and press the Download button. Your download will begin.
You may need to log in to your Oracle account one more time before the download starts.
Once the file has downloaded, you’ll need to transfer it to your server. On your local machine, upload
the file to your server. On macOS, Linux, or Windows using the Windows Subsystem for Linux, use
the scp command to transfer the file to the home directory of your sammy user. The following
command assumes you’ve saved the Oracle JDK file to your local machine’s Downloads folder
Once the file upload has completed, return to your server and add the third-party repository that will
help you install Oracle’s Java.
Next, import the signing key used to verify the software you’re about to install:
$ sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys
EA8CACC073C3DB2A
Output
gpg: key EA8CACC073C3DB2A: public key "Launchpad PPA for Linux
Uprising" imported
gpg: Total number processed: 1
gpg: imported: 1
Then use the add-apt-repository command to add the repo to your list
of package sources:
Output
Oracle Java 11 (LTS) and 16 installer for Ubuntu (21.04, 20.10,
20.04, 18.04, 16.04 and 14.04), Pop!_OS, Linux Mint and Debian.
Java binaries are not hosted in this PPA due to licensing. The
packages in this PPA download and install Oracle Java, so a working
Internet connection is required.
The packages in this PPA are based on the WebUpd8 Oracle Java PPA
packages: https://launchpad.net/~webupd8team/+archive/ubuntu/java
. . .
Update your package list to make the new software available for installation:
The installer will look for the Oracle JDK you downloaded in /var/cache/oracle-jdk11-installer-local.
Create this directory and move the Oracle JDK archive there:
The installer will first ask you to accept the Oracle license agreement. Accept the agreement, then the
installer will extract the Java package and install it.
Now let’s look at how to select which version of Java you want to use.
Managing Java
You can have multiple Java installations on one server. You can configure which version is the default for
use on the command line by using the update-alternatives command.
This is what the output would look like if you’ve installed both versions of Java in this tutorial:
Output
There are 2 choices for the alternative java (providing
/usr/bin/java).
Choose the number associated with the Java version to use it as the default, or press ENTER to leave the
current settings in place.
You can do this for other Java commands, such as the compiler (javac):
Other commands for which this command can be run include, but are not limited
to: keytool, javadoc and jarsigner.
Many programs written using Java use the JAVA_HOME environment variable to determine the Java
installation location.
To set this environment variable, first determine where Java is installed. Use the update-
alternatives command:
This command shows each installation of Java along with its installation path:
Output
There are 2 choices for the alternative java (providing
/usr/bin/java).
At the end of this file, add the following line, making sure to replace the highlighted path with your own
copied path, but do not include the bin/ portion of the path:
/etc/environment
JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"
Modifying this file will set the JAVA_HOME path for all users on your system.
Now reload this file to apply the changes to your current session:
$ source /etc/environment
$ echo $JAVA_HOME
Other users will need to execute the command source /etc/environment or log out and log back in to
apply this setting.
Conclusion
In this tutorial you installed multiple versions of Java and learned how to manage them. You can now
install software which runs on Java, such as Tomcat, Jetty, Glassfish, Cassandra or Jenkins.