Feb 13, 2024 5 min read

How to Install OpenJDK on Ubuntu 22.04

Install OpenJDK on Ubuntu 22.04 2ith our step-by-step tutorial. OpenJDK is a free and open-source Java SE implementation.

Install OpenJDK on Ubuntu 22.04
Install OpenJDK on Ubuntu 22.04
Table of Contents


Before we begin talking about how to install OpenJDK on Ubuntu 22.04, let's briefly understand – What is OpenJDK?

OpenJDK, short for Open Java Development Kit, is a free and open-source implementation of the Java Platform, Standard Edition (Java SE). It provides developers with the tools, libraries, and runtime environment needed to create and run Java applications. OpenJDK is maintained and supported by the Java community, ensuring continuous updates and improvements.

Its open nature encourages collaboration, innovation, and the sharing of knowledge among developers worldwide. With its wide adoption and compatibility, OpenJDK empowers developers to build reliable, secure, and high-performance Java applications for various platforms and devices.

In this tutorial, you will install OpenJDK on Ubuntu 22.04. We will also address a few FAQs on how to install OpenJDK on Ubuntu 22.04.

Advantages of OpenJDK

  1. Open Source: OpenJDK is freely available for developers to use, modify, and distribute, promoting an open and collaborative environment.
  2. Community Support: Being community-driven, OpenJDK benefits from continuous updates, bug fixes, and enhancements from a diverse and active community of developers worldwide.
  3. Compatibility: OpenJDK is built on the Java SE standard, ensuring compatibility with Java applications across different platforms and devices.
  4. Security: The open nature of OpenJDK enables community scrutiny, resulting in frequent security updates that keep Java applications safe from vulnerabilities.
  5. Performance: OpenJDK offers high-performance capabilities, including Just-in-Time (JIT) compilation and efficient garbage collection, for optimized execution of Java applications.

How to Install OpenJDK

Installing the various OpenJDK releases is possible with apt. Currently, the majority of developers work with either Java Platform Release 11 or Release 17. While release 18 is the most recent release, these are also LTE releases.

Note: If you want to install an OpenJDK release that isn't accessible via apt, get the relevant tar.gz file from the OpenJDK archive.

Make sure you have updated the apt libraries.

sudo apt update && sudo apt upgrade -y

Verify if Java is already installed.

java -version


Command 'java' not found

Use apt to install the preferred version of OpenJDK. When Ubuntu asks if you want to continue with the installation, select y. To install OpenJDK release 17's complete JDK platform, use the following command. You can follow the instructions below to install just the JRE component.

Note: Use the command sudo apt-get install openjdk-11-jdk to install OpenJDK release 11. To install release 18, type sudo apt-get install openjdk-18-jdk. Use the apt-cache search openjdk command to search the apt libraries for every edition of OpenJDK that is available.
sudo apt-get install openjdk-17-jdk

(Optional) Use the following command to install OpenJDK's JRE component only. Replace 17 with the relevant release number in order to install a release other than OpenJDK release 17.

sudo apt-get install openjdk-17-jre

Verify that the appropriate Java release is installed.

java -version


openjdk version "17.0.3" 2022-04-19
OpenJDK Runtime Environment (build 17.0.3+7-Ubuntu-0ubuntu0.22.04.1)
OpenJDK 64-Bit Server VM (build 17.0.3+7-Ubuntu-0ubuntu0.22.04.1, mixed mode, sharing)

Verify the Java compiler's release if the entire JDK platform is installed.

javac -version


javac 17.0.3

Set the Environment Variables for Java

Even though Java is already installed, more setup is necessary. OpenJDK can be found by other applications and is made easier to use by setting a few environment variables. To configure the Java environment variables, take the following actions:

  1. Add the following definitions to the end of the .bashrc file in your home directory. Once the changes are made, save the file.
Alternatively, add the following modifications to /etc/environment to set these values for all system users.
vi ~/.bashrc


File: ~/.bashrc

export JAVA_HOME=$(dirname $(dirname $(readlink -f $(which java))))
export PATH=$PATH:$JAVA_HOME/bin

2. To implement the modifications, source the .bashrc file.

source ~/.bashrc

3. Echo JAVA_HOME and PATH to verify they are set correctly. Depending on the release, the $JAVA_HOME value should resemble /usr/lib/jvm/java-17-openjdk-amd64. Additionally, the $PATH variable needs to have this value added at the end.

echo $PATH

Test the OpenJDK Installation

Writing a basic Java application is the best way to test the installation. Launch the program to verify that the JRE is operating properly. Try compiling the application as well if the complete JDK is also installed.

Run a Java Application Using the OpenJDK JRE

Java code can be executed by the JRE, but it cannot compile an application. Write a little Java program and run it to confirm that the JRE is operational.

Use a text editor to write an example of a HelloWorld program. Make a HelloWorld class in a file called HelloWorld.java. Hello World from Java! should be printed to the console by the class's Main routine. The file ought to look something like this code sample:

File: HelloWorld.java

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World from Java!");

Run HelloWorld.java in the same directory using the OpenJDK JRE.

java HelloWorld.java

To confirm that the installation was successful, check the console output. It should say Hello World from Java! There may be a brief wait before the program launches because JRE needs to first compile the program into bytecode.


Hello World from Java!

Compile a Java Application Using the JDK

A complete development environment is the JDK. For quicker execution, it can translate Java code into Java bytecode. A standalone Java class file that can be shared with others is produced by this process. The steps listed below will help you compile the HelloWorld.java test application:
Use the Java compiler to compile the file from the source code directory.

javac HelloWorld.java

Verify that there is now a HelloWorld.class file in the same directory.

ls -l HelloWorld.class


-rw-rw-r-- 1 userid userid 436 May 31 08:33 HelloWorld.class

Using the OpenJDK JRE, launch the compiled HelloWorld class file. The output of should say Hello World Java!.

The HelloWorld.java source file was executed by the JRE in the preceding section. HelloWorld, the compiled class file, is being executed this time.
java HelloWorld


Hello World from Java!

FAQs to Install OpenJDK on Ubuntu 22.04

Can I install multiple versions of OpenJDK on Ubuntu 22.04?

Yes, you can install multiple versions of OpenJDK on Ubuntu 22.04. Use the sudo apt install openjdk-<version>-jdk command, replacing <version> with the desired version number.

How do I set the default version of OpenJDK on Ubuntu 22.04?

You can set the default version of OpenJDK by running the sudo update-alternatives --config java command. It will provide a list of installed Java versions to choose from.

Is OpenJDK free to use on Ubuntu 22.04?

Yes, OpenJDK is free to use on Ubuntu 22.04 as it is an open-source implementation of Java.

Can I install OpenJDK on other Ubuntu versions besides 22.04?

Yes, you can install OpenJDK on other Ubuntu versions as well. Ensure the appropriate package name and version for your Ubuntu release.

Can I use OpenJDK for commercial purposes?

Yes, OpenJDK can be used for commercial purposes without any licensing fees or restrictions.

Is OpenJDK compatible with Java applications?

Absolutely! OpenJDK is compatible with Java applications developed using the Java SE standard, ensuring seamless execution.

How do I uninstall OpenJDK from Ubuntu 22.04?

To remove OpenJDK from Ubuntu 22.04, use the command sudo apt remove openjdk-11-jdk in the terminal.


We hope this tutorial helped you understand how to install OpenJDK on Ubuntu 22.04.

If you have any queries, please leave a comment below, and we’ll be happy to respond to them for sure.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.