How to Install Java OpenJDK and Oracle JDK on Ubuntu 22.04

Java is one of the most powerful programming languages. It is very popular among web developers due to its simplicity and robustness. Generally, it is used to develop computer, video, and mobile games.

OpenJDK is an open-source Java edition and is available for free use. It supports multiple operating systems like Windows, Linux, Solaris, macOS, etc.

Oracle Java is a programming language developed by Oracle corporation. It gives customers enterprise features that minimize the costs of deploying and maintaining their Java-based applications.

This post will show you how to install OpenJDK and Oracle JDK on Ubuntu 22.04.

Prerequisites

  • A server running Ubuntu 22.04.
  • A root password is configured on the server.

OpenJDK vs Oracle JDK

First, let's talk about which of these two JDK might fit you best.

Java is an incredibly popular programming language, and both OpenJDK and Oracle JDK are different implementations.

OpenJDK is an open-source version of the Java Development Kit (JDK) originally developed by Sun Microsystems and Oracle. It's a free and open-source implementation of the Java platform Standard Edition (Java SE). The Java Community Process (JCP) is responsible for setting standards specific to OpenJDK.

On the other hand, Oracle JDK is a proprietary implementation of the same Java SE Platform Edition developed by Oracle, which acquired Sun Microsystems in 2010. It's commercial, so users have to pay a license fee for its use. Oracle JDK includes additional features that aren't available with OpenJDK, including better performance and support from Oracle itself.

Both flavors of the JDK have advantages and disadvantages depending on your needs and budget. Suppose you need more control over your development environment or access to additional features, such as support from Oracle itself. In that case, you may want to go with Oracle JDK instead of OpenJDK.

Install OpenJDK on Ubuntu

When writing this article, Java 18 is the latest LTS release available for installation. Ubuntu 22.04 default repository provides the OpenJDK version 18, 17, 11, and 8 packages. You can use the APT package manager to install your desired Java version on your system.

To install OpenJDK 8, run the following command:

apt install openjdk-8-jdk

To install OpenJDK 11, run the following command:

apt install openjdk-11-jdk

To install OpenJDK 18, run the following command:

apt install openjdk-18-jdk

After the installation, verify the active version of OpenJDK using the following command:

java -version

You should see the following output:

openjdk version "18-ea" 2022-03-22
OpenJDK Runtime Environment (build 18-ea+36-Ubuntu-1)
OpenJDK 64-Bit Server VM (build 18-ea+36-Ubuntu-1, mixed mode, sharing)

Install Oracle JDK

At the time of writing this article, Oracle Java 18 and Java 17 are available to download without any commercial license. First, install the required dependencies using the following command:

apt install -y libc6-x32 libc6-i386

Next, visit the Oracle download page and download the latest version of Oracle JDK with the following command:

wget https://download.oracle.com/java/18/latest/jdk-18_linux-x64_bin.deb

Once the download is completed, install the downloaded version using the following command:

apt install ./jdk-18_linux-x64_bin.deb

Once installed, you can set the Oracle JDK 18 as the default version using the following command:

update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk-18/bin/java 100

Switch Between Different Java Versions

If multiple Java versions are installed on your server, you can easily switch between Java versions in your system as per your requirement.

To change the default Java version, run the following command:

update-alternatives --config java

You will be asked to set the default Java version as shown below:

There are 4 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                            Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-18-openjdk-amd64/bin/java      1811      auto mode


  1            /usr/lib/jvm/java-11-openjdk-amd64/bin/java      1111      manual mode
  2            /usr/lib/jvm/java-18-openjdk-amd64/bin/java      1811      manual mode
  3            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081      manual mode
  4            /usr/lib/jvm/jdk-18/bin/java                     100       manual mode

Press  to keep the current choice[*], or type selection number: 4

Type 4 and press the Enter key to set the Oracle JDK as the default Java version:

Next, verify the default Java version using the following command:

java -version

You should see the following output:

java version "18.0.2" 2022-07-19
Java(TM) SE Runtime Environment (build 18.0.2+9-61)
Java HotSpot(TM) 64-Bit Server VM (build 18.0.2+9-61, mixed mode, sharing)

Conclusion

In this post, we explained how to install OpenJDK and Oracle JDK on Ubuntu 22.04. I hope you can now easily install your preferred Java versions on your server. Feel free to ask me if you have any questions.

Share this page:

1 Comment(s)