Install Java on Ubuntu 22.04

October 21, 2022 · 0 min read
Install Java on Ubuntu 22.04
Using apt, you will install different versions of the Java Runtime Environment (JRE) and the Java Developer Kit (JDK) in this guide. You will install both OpenJDK and the standard JDK from Oracle. You will then choose which version you want to utilize for your projects. When you're completed, you'll be able to write software using the JDK and run it with the Java Runtime.

Install Java

A Java installation has two major components. The JDK includes necessary software tools for Java development, such as a compiler and debugger. The JRE is required to run Java apps. Furthermore, there are two major Java installation choices to choose from. OpenJDK is the open-source Java implementation that comes with Ubuntu. Oracle JDK is the original version of Java and is fully supported by Oracle, the Java developers.

Oracle officially recognizes both of these versions. Both are created by Oracle, however, due to its open-source nature, OpenJDK includes community contributions. However, as Oracle explains, the two alternatives are now functionally similar to Java 11. The decision between which to install boils down to selecting the proper licensing for your situation. Furthermore, OpenJDK allows you to install the JRE individually, whereas OracleJDK includes the JRE.

Install the Standard JRE/JDK

The version included with Ubuntu is one option for installing Java. Open JDK 11, an open-source variation of the JRE and JDK, is included by default in Ubuntu 22.04.
First, update your apt package index before installing the OpenJDK version of Java:

sudo apt update

Check to see if Java is already installed:

java -version

If Java is not currently installed, the following message will be displayed:

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

sudo apt install default-jre              # version 2:1.11-72build1, or
sudo apt install openjdk-11-jre-headless  # version 11.0.14+9-0ubuntu2
sudo apt install openjdk-17-jre-headless  # version 17.0.2+8-1
sudo apt install openjdk-18-jre-headless  # version 18~36ea-1
sudo apt install openjdk-8-jre-headless   # version 8u312-b07-0ubuntu1

To install the JRE from OpenJDK 11, use the following command:

sudo apt install default-jre

Almost any Java software can be executed using the JRE.
Check the installation with:

java -version

You'll get something like this as output:

openjdk version "11.0.14" 2022-01-18
OpenJDK Runtime Environment (build 11.0.14+9-Ubuntu-0ubuntu2)
OpenJDK 64-Bit Server VM (build 11.0.14+9-Ubuntu-0ubuntu2, mixed mode, sharing)

In order to compile and run some Java-based software, you may need the JDK in addition to the JRE. Execute the following command to install the JDK, which will also install the JRE:

sudo apt install default-jdk

Check the version of javac, the Java compiler, to ensure that the JDK is installed:

javac -version

You'll get the following results:

javac 11.0.14

Managing Java

Java installs can coexist on the same server. The update-alternatives command can be used to specify which version is the default for use on the command line.

sudo update-alternatives --config java

If you followed along with this lesson, you should get the following output:

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-11-oracle/bin/java          1091      manual mode

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

Select the number linked with the Java version as the default, or click ENTER to keep the current settings.
This is also applicable to other Java commands, such as the compiler (javac):

sudo update-alternatives --config javac

Other commands for which this command can be run include, but are not limited to: keytooljavadoc, and jarsigner.

Set JAVA_HOME Environment Variable

Many Java programs utilize the JAVA HOME environment variable to determine the location of the Java installation.
Determine where Java is installed before setting this environment variable. Use the update-alternatives command as follows:

sudo update-alternatives --config java

This command displays each Java installation along with its installation path:

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-11-oracle/bin/java          1091      manual mode

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

The installation paths in this situation are as follows:

  • /usr/lib/jvm/java-11-openjdk-amd64/bin/java is the location of OpenJDK 11.
  • /usr/lib/jvm/java-11-oracle/jre/bin/java is the location of Oracle Java.

Copy the installation path from your selected installation. Then, using nano or your preferred text editor, navigate to /etc/environment:

sudo nano /etc/environment

Add the following line at the end of this file, making sure to replace the highlighted path with your own copied path and omitting the bin/ section of the path:

JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"

Modifying this file will change the JAVA HOME path for all users on your system.
Save the file and close the editor.
Reload this file to apply the changes to your current session:

source /etc/environment

Check that the environment variable is set:

echo $JAVA_HOME

You'll notice the path you just created:

/usr/lib/jvm/java-11-openjdk-amd64

To apply this setting, other users must run the command source /etc/environment or log out and log back in.

Conclusion

Now, you how to install and manage several versions of Java. You can now install Java-based software such as Tomcat, Jetty, Glassfish, Cassandra, or Jenkins.