How to install Java 17 in Ubuntu 22.04

In this guide we are going to explore how to install Java Runtime Environment (JRE) and the Java Developer Kit (JDK) in Ubuntu 22.04.

Java and the JVM (Java’s virtual machine) are required for many kinds of software, including TomcatJettyGlassfishCassandra and Jenkins.

Java is a high-level, class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible. Java was developed by Sun Microsystems (which is now the subsidiary of Oracle) in the year 1995. James Gosling is known as the father of Java. 

Related Content:

# Prerequisites

  • Up to date Ubuntu 22.04 system
  • Root access or user with sudo access
  • Internet access from the server

# Table of content

  1. Ensuring that the server is up to date
  2. Installing java
  3. Testing the installation
  4. Toggling multiple Java versions
  5. Configuring the Environmental Variable

# Ensuring that the server is up to date

Before proceeding, let us make sure that the server packages and repositories are up to date. Use these commads to achieve the goal:

sudo apt update
sudo apt -y upgrade

If there are packages to upgrade, the above comand may take a couple of minutes.

Let us also install some common packages that we might need. I use vim text editor, please make sure its installed using this command:

sudo apt install -y vim

# Installing Java

Check whether java is installed in your sytem.

$ java -version

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

sudo apt install openjdk-11-jre-headless  # version 11.0.11+9-0ubuntu2~22.04, or
sudo apt install default-jre              # version 2:1.11-72
sudo apt install openjdk-16-jre-headless  # version 16.0.1+9-1~22.04
sudo apt install openjdk-8-jre-headless   # version 8u292-b10-0ubuntu1~22.04
sudo apt install openjdk-13-jre-headless  # version 13.0.7+5-0ubuntu1~22.04
sudo apt install openjdk-17-jre-headless  # version 17+35-1~22.04

If you see java: command not found then it means that java is not installed. If java is installed, that command will print out the java version

Ubuntu 22.04’s default repository included Java 17. This is the easiest way to install the JDK using the apt package manage.

You can search java using this command:

$ sudo apt-cache search openjdk | grep 17
openjdk-17-dbg - Java runtime based on OpenJDK (debugging symbols)
openjdk-17-demo - Java runtime based on OpenJDK (demos and examples)
openjdk-17-doc - OpenJDK Development Kit (JDK) documentation
openjdk-17-jdk - OpenJDK Development Kit (JDK)
openjdk-17-jdk-headless - OpenJDK Development Kit (JDK) (headless)
openjdk-17-jre - OpenJDK Java runtime, using Hotspot JIT
openjdk-17-jre-headless - OpenJDK Java runtime, using Hotspot JIT (headless)
openjdk-17-jre-zero - Alternative JVM for OpenJDK, using Zero
openjdk-17-source - OpenJDK Development Kit (JDK) source files

Run the following command to install Java 17 on Ubuntu 22.04. We will install both JDK and JRE in order to run Java applications and applets.

sudo apt install openjdk-17-jdk openjdk-17-jre -y

Once the installation is completed, run the following command in order to check if it works properly. If everything works fine, you should get an output similar to the one shown below.

$ java -version
openjdk version "17.0.4" 2022-07-19
OpenJDK Runtime Environment (build 17.0.4+8-Ubuntu-122.04)
OpenJDK 64-Bit Server VM (build 17.0.4+8-Ubuntu-122.04, mixed mode, sharing)

To verify if the JDK is installed properly, we will check the version of javac, which is the Java compiler.

$ javac -version
javac 17.0.4

# Testing the installation

Let us test that Java 17 is installed properly in this step by creating a simple script that prints out Java is installed properly.

Create the directory and switch to it

mkdir simple-app
cd simple-app

I use vim to create and edit text files in my system. Use the text editor of your choice in this step. I will create a file using this command:

sudo vim

Add these content to the file

public class Simple{
   public static void main(String[] args) {
      System.out.println("Citizix - Java is installed properly");

Now that we have created our program, we need to compile the Java source code into bytecode (class file) using the javac compiler.


You will get a new file called Simple.class, which is the compiled Java class file.

$ ls

The command above will only compile the Java source code into bytecode. In order to run the program, we run the java command with the name of our class file as an argument.

java Simple

If everything works well, you’ll see a message “Citizix - Java is installed properly” on the screen.

$ java Simple
Citizix - Java is installed properly

# Toggling multiple Java versions

If you have multiple versions of Java installed in your system, you can configure which version the java command defaults to.

Use this command to configure that

sudo update-alternatives --config java

You will be given a list of available java installations. In this case I have both java 11 and java 17 installed. The default version is marked with an asterisk(*). You can opt to use a different version by entering the number in the prompt.

$ sudo update-alternatives --config java
There are 2 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                         Priority   Status
* 0            /usr/lib/jvm/java-17-openjdk-amd64/bin/java   1711      auto mode
  1            /usr/lib/jvm/java-11-openjdk-amd64/bin/java   1111      manual mode
  2            /usr/lib/jvm/java-17-openjdk-amd64/bin/java   1711      manual mode

Press <enter> to keep the current choice[*], or type selection number: 1
update-alternatives: using /usr/lib/jvm/java-11-openjdk-amd64/bin/java to provide /usr/bin/java (java) in manual mode

In the above, I entered 1 to the promp to choose Java 11. Let us confirm the java version now:

$ java -version
openjdk version "11.0.11" 2021-04-20
OpenJDK Runtime Environment (build 11.0.11+9-Ubuntu-0ubuntu2.22.04)
OpenJDK 64-Bit Server VM (build 11.0.11+9-Ubuntu-0ubuntu2.22.04, mixed mode, sharing)

# Configuring the Environmental Variable

Some Java applications require the JAVA_HOME environmental variable in order to run properly. Some programs are very specific in how they are executed. If JAVA_HOME isn’t set, you’ll get an error. Setting JAVA_HOME will prevent this problem from arising.

To check which Java installations and paths exist, use this command:

sudo update-alternatives --config java

In my case, I get the Java installation binary path to be this /usr/lib/jvm/java-17-openjdk-amd64/bin/java

We are going to set this path in an enviroment file /etc/environment where all the apps can access.

Open the file with vim:

sudo vim /etc/environment

Now we need to add a line exporting the Java home to the file like this export JAVA_HOME=... making the value to be the path we got for our Java installation:

export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64/bin/java

This file will set the JAVA_HOME path of OpenJDK 17 as environmental variables for all users in the system.

To apply the change, run the following command.

source /etc/environment

To verify that the path variable has been applied, run the following command.


The path should be set to the path of OpenJDK 17, which you have copied above. This is the value in my case:

$ echo $JAVA_HOME

From now on, you don’t have to set the JAVA_HOME path every time when using Java programs. Also, if you want to use other OpenJDK versions instead of 17, adapt the path accordingly.

# Conclusion

In this tutorial, you have learned how to install Java 17 on a Ubuntu 22.04 system. If you need further information about the Java language, please refer to its official documentation.

Last updated on Mar 20, 2024 17:19 +0300
comments powered by Disqus
Citizix Ltd
Built with Hugo
Theme Stack designed by Jimmy