Step-by-Step Guide: How to Install Java on Ubuntu 22.04

Java is a globally recognized programming language for creating robust and scalable applications. It was introduced in 1995 and has become one of the most widely used options in the industry. This programming language is employed in various domains, including desktop and Android app development, gaming, big data processing, and web applications. So, even though there are many other programming languages available, Java's high-level, versatile, and object-oriented nature maintains its significance, especially in building enterprise-grade applications, making it a preferred choice for developers.

Important Features of Java 

  • Java, known for its speed and reliability, builds applications across diverse domains such as game development, web and mobile app creation, chatbot development, and the development of enterprise-level software.
  • A key advantage of Java lies in its platform independence, allowing it to execute seamlessly on any operating system. This quality makes Java an excellent choice for organizations aiming to build high-performance cross-platform applications.

In this article, we will demonstrate how to install Java on Ubuntu 22.04. 

Prerequisites

To install Java on the Ubuntu system, you will need to meet the following requirements:

  1. Set up the Ubuntu 22.04 server on your machine or install it inside a VirtualBox.
  2. You should have a root account. In the case of non-root users, you must have “sudo” privileges to execute the administration commands on the Ubuntu distribution.

Installing Java Ubuntu 22.04

Before you start the Java installation, check if you have Java already installed on your system. You can check which Java version you have by using the command below:

$ java --version

step-by-step guide: how to install java on ubuntu 22.04

The above result shows that Java is not installed on this system. If you receive this terminal output, you need to install Java. But before you do, you should have basic knowledge about the Java components. 

Java installations consist of two primary components - the first component is the Java JDK (Java Development Kit), and the second is the JRE (Java Runtime Environment).  The JDK equips developers with essential software tools like compilers and debuggers for Java application development, while the JRE is responsible for executing Java programs.

Methods to Install Java on Ubuntu

You can install Java on Ubuntu using two different methods:

  1. Install Java on Ubuntu using OpenJDK.
  2. Install Oracle Java 17 on Ubuntu using the PPA repository.

Method 1: Installing Java on Ubuntu from OpenJDK

One method to install Java on Ubuntu is using the OpenJDK. In this method, OpenJDK offers a comprehensive set of Java development tools used for developing Java-based applications and microservices. This includes essential components like the Java compiler, Java Runtime Environment (JRE), and the Java class library, making it a one-stop solution for Java development needs.

In the section below, we will demonstrate how you can install Java on Ubuntu from OpenJDK.

Installing Java on Ubuntu 22.04 from OpenJDK includes the following steps:

Step 1: Update Ubuntu Repositories

To install the OpenJDK Java version, first update your system apt repositories using the command below:

$ sudo apt update

step-by-step guide: how to install java on ubuntu 22.04

After that, confirm the Java installation:

$ java --version

Step 2: Search OpenJDK using Apt Cache

The Ubuntu 22.04 repository, by default, includes the OpenJDK. It is an open-source version of JDK and JRE. Therefore, you can search the OpenJDK in the apt repository using the following command:

$ sudo apt-cache search openjdk

step-by-step guide: how to install java on ubuntu 22.04

You can see all the OpenJDK versions in the terminal window. At the time of writing this article, OpenJDK 20 is the most recent version. But in this guide, we will install the OpenJDK 17 LTS (Long Term Support) version on Ubuntu 22.04. 

Step 3: Install OpenJDK-17 Java on Ubuntu

Choose the suitable version of OpenJDK and install it using the apt repository. 

$ sudo apt install openjdk-17-jdk

step-by-step guide: how to install java on ubuntu 22.04

Now, press ‘y’ and ‘Enter’ to proceed with the installation.

The above command will install the complete JDK platform of the OpenJDK 17 version. This is optional. However, if you only want to install the OpenJDK JRE component, execute the following command:

$ sudo apt install openjdk-17-jre

Step 4: Verify Java Installation

Now, verify if Java is correctly installed on Ubuntu or not. Check the currently installed Java version with the following command:

$ java -version

step-by-step guide: how to install java on ubuntu 22.04

Check the Java compiler version to verify that the full JDK platform is installed on your system:

$ javac -version

step-by-step guide: how to install java on ubuntu 22.04

Method 2: Installing Oracle JDK 17 using PPA

You can install Oracle JDK, which offers Oracle's Java implementation as an alternative to OpenJDK. The OpenJDK, which is an open-source implementation of Java, is readily available and comes bundled with Ubuntu. On the other hand, another option for installing Java is Oracle JDK, which is the licensed Java version maintained exclusively by Oracle, the developers of Java.

You need to execute the following steps to install Oracle JDK on Ubuntu 22.04:

Step 1: Add Oracle Java PPA Linux Uprising Repository

Using the Oracle Java repository, you can easily install Oracle Java 17 on the Ubuntu system. To do this, you need to add the PPA Linux uprising Oracle Java repository to your Ubuntu system by running this command:

$ sudo add-apt-repository ppa:linuxuprising/java

step-by-step guide: how to install java on ubuntu 22.04

A prompt will appear on the terminal, and you need to press “Enter” to continue the process.

Step 2: Update Packages Index

Once you add the Oracle repository, it is recommended that you refresh your system repositories using the “apt update” command:

$ sudo apt update

Step 3: Install Oracle Java 17 on Ubuntu 22.04

In this step, you will install the Oracle JDK on your system by executing the following command:

$ sudo apt install oracle-java17-installer

step-by-step guide: how to install java on ubuntu 22.04

During the installation, you will need to accept the ‘Oracle No-Fee Terms and Conditions’ license. Press the “Tab” key and then hit “Enter.”

step-by-step guide: how to install java on ubuntu 22.04

Again, select “Yes” to confirm the license agreement terms and conditions. 

step-by-step guide: how to install java on ubuntu 22.04

It will take a few seconds to complete the Oracle Java 17 installation. Once the installation is completed, check the installed Java version using the “java -version” command.

step-by-step guide: how to install java on ubuntu 22.04

How to Test Java Installation on Ubuntu 22.04?

Once Java is installed on Ubuntu 22.04, you can test the installation. To verify the Java installation, create a file in a text editor and paste the following lines of Java code:

public class TestProgram {

    public static void main(String[] args) {

        System.out.println("Test Java Installation!");

    }

}

Save this file with “TestProgram.java,” which will be the same as the class name. Now, open the terminal and run the above code by executing the following command:

step-by-step guide: how to install java on ubuntu 22.04

As you can see from the screenshot above, Java has been successfully installed and running on this system. Using this guide, you can also install Java on the remote servers and BlueVPS.

How to Configure Default Java Version on Ubuntu 22.04?

It is possible to have multiple Java installations exist on your system without significant problems. However, in some cases, you may need to choose a specific Java installation as the default one, depending on the requirements of your projects.

This can be achieved using the “update-alternatives” command, which empowers you to both list and select the default Java version on your system.

$ sudo update-alternatives --config java

After executing the above command, you will be asked to type a number that you want to set as the default Java version. Here, we have written “1” to set Java as the default version.

step-by-step guide: how to install java on ubuntu 22.04

How to Set  Environment Variable JAVA_HOME for Java on Ubuntu 22.04?

Although Java is already installed, additional configuration steps are necessary to optimize its usage and enable other applications to locate it seamlessly. One of the Java path configurations involves defining specific environment variables. 

Among these, the JAVA_HOME environment variable specifies the installation directory of the Java Runtime Environment (JRE). This step is not mandatory. However, configuring this variable is highly recommended since numerous Java-based applications rely on it to find the exact Java location.

First, check the Java path by executing the following command:

$ sudo update-alternatives --config java

step-by-step guide: how to install java on ubuntu 22.04

The above command will display the Java path on the terminal screen. To set these values for all system users, add the following changes to the “/etc/environment:”

To set the JAVA_HOME environment variable for all system users, you need to make changes in the configuration file, which is located at “/etc/environment.” Open this file in your favorite text editor:

$ sudo nano /etc/environment

Now, set the Path for the JAVA_HOME environment variable. To do this, paste the line below in the “/etc/environment:”

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

To make changes in the configuration file, run the following command:

$ source /etc/environment

Now, display the updated Java path using the “echo” command:

$ echo “$JAVA_HOME”

step-by-step guide: how to install java on ubuntu 22.04

Conclusion

In this article, we showed you how to install Java on Ubuntu 22.04. Also, we demonstrated how to test the Java installation and configure the JAVA_HOME environment variable using the command line.

But before you start the installation process, you need to choose an appropriate Java installation for your system. In this case, you have two main options to consider: OpenJDK and Oracle JDK. Once you’ve successfully installed Java on Ubuntu, you can run Java code or Java-based applications on your Ubuntu system. 

We hope you find the information we provided in this article useful. If you want to learn more about Java, you can visit the official Java website. Here, you can also download Java manually based on your system architecture and running distribution. Thanks for reading!