How to Install Oracle Java 17 on Ubuntu 22.04 or 20.04

Java remains a cornerstone in the vast landscape of programming languages, with its versatility and widespread adoption. Choosing the right Java Development Kit (JDK) is crucial for developers venturing into Java development on Ubuntu systems. This guide will spotlight the Oracle Java JDK, specifically focusing on how to install Oracle Java 17 on Ubuntu 22.04 Jammy Jellyfish or Ubuntu 20.04 Focal Fossa.

Oracle’s Java JDK is a comprehensive toolkit for Java developers, encompassing the Java Runtime Environment (JRE) and the Java Development Kit (JDK). This combination ensures developers have all the necessary tools to craft, compile, and execute Java applications.

A common query among developers is the distinction between Oracle JDK and OpenJDK. While OpenJDK is the open-source rendition of the JDK, Oracle’s version is the proprietary counterpart. Functionally, they are almost mirror images; however, Oracle JDK boasts additional features and support services that might appeal to certain developers.

Opting for Oracle JDK on Ubuntu offers several advantages, with Long-Term Support (LTS) being paramount. Oracle JDK’s long-term support, especially for versions like Java 17 LTS, ensures prolonged access to updates and patches compared to OpenJDK.

Oracle Java 17 LTS Highlights:

  • Robust Security: Java 17 LTS introduces enhanced security measures, including refined X.509 certificate management and fortified cryptographic algorithms.
  • Optimized Performance: Oracle JDK is fine-tuned for peak performance, integrating features like the Just-In-Time (JIT) compiler and advanced garbage collection.
  • Developer-Centric Tools: Oracle JDK is bundled with potent tools, encompassing the JDK, JavaFX, and the renowned NetBeans Integrated Development Environment (IDE).
  • Seamless Compatibility: Oracle JDK ensures high compatibility with the latest Java iterations, guaranteeing fluid application performance across platforms.
  • Dedicated Support: Oracle extends support services for its JDK, encompassing regular updates, patches, and bug resolutions.
  • Migration Assistance: Transitioning from an older Java version? Oracle offers migration support to facilitate a smooth shift to the latest iteration.

With this foundational understanding, the subsequent sections of this guide will delve into the detailed steps to install Oracle Java 17 on your Ubuntu system, ensuring you’re equipped with the latest Java capabilities.

Note: For users wishing to install the OpenJDK 17 version, please see the tutorial How to Install OpenJDK 17 on Ubuntu 22.04 or 20.04.

Step 1: Update Ubuntu Before Java 17 Installation on Ubuntu

To ensure that your Ubuntu operating system runs smoothly and efficiently to avoid conflicts with new software installations, it is essential to run the following command before installing new packages:

sudo apt update && sudo apt upgrade

Step 2: Select Java 17 Installation Method on Ubuntu

Method 1: Install Java 17 with PPA Method

One of the available options for installing Java 17 JDK is through the ppa:linuxuprising repository, which currently features the Java 17 package with the following command:

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

Before continuing with the installation, it’s essential to run an APT update to confirm that the recently added Oracle Java 17 Launchpad PPA is accurately reflected in the sources list file and the package cache is current. This can be achieved by executing the following command in the terminal.

sudo apt update

Now, you can install Oracle Java 17 with the following command.

sudo apt install oracle-java17-installer oracle-java17-set-default

During the installation of the Oracle JDK, it is essential to note that you must accept Oracle’s terms before proceeding. This step is crucial in ensuring you comply with the licensing agreement and have the necessary software permissions. Failure to accept these terms may result in errors or other issues during installation.

Example of pop-ups of Oracle’s terms:

Screenshot showing the terms pop-up during Oracle JDK 17 installation on Ubuntu 22.04 or 20.04 LTS.
What to expect: Terms pop up when installing Oracle JDK 17 on Ubuntu 22.04 or 20.04 LTS.
Screenshot of the "Accept Terms" option for Oracle JDK 17 installation on Ubuntu 22.04 or 20.04 LTS.
Finalizing installation: Accepting the terms of Oracle JDK 17 on Ubuntu 22.04 or 20.04 LTS.

Once the installation is complete, you can verify it was successful using the –version command. This command will display the version of Java currently installed on your system, allowing you to confirm that the installation was successful and that the correct version of Java is in use. To check the version of Java on your system, enter the following command in your terminal:

java --version

Example output:

java 17.0.5 2022-10-18 LTS
Java(TM) SE Runtime Environment (build 17.0.5+9-LTS-191)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.5+9-LTS-191, mixed mode, sharing)

By running this command, you can ensure that the installation of the Oracle JDK was successful and that your development environment is ready for use.

Method 2: Install Java 17 via .deb on Ubuntu

Another method for installing Oracle Java 17 is using the Debian package. This approach is a more straightforward option that provides an easy and efficient way to install the Java JDK 17 using the dpkg command. This powerful command allows you to manage and install .deb packages, making it an excellent option for installing Oracle Java 17 on your Ubuntu system.

To begin, you will need to download the .deb package from Java’s download page or copy and paste the link into a wget command in your terminal. This will download the necessary files and dependencies required to install Oracle Java 17 through the dpkg command. To download the .deb package using wget, enter the following command in your terminal:

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

You can download the necessary package files by running this command, allowing you to install Oracle Java 17 using the dpkg command.

To install Java using the dpkg command, you will need to navigate to the directory where the .deb package is located and then run the following command in your terminal:

sudo dpkg -i jdk-17_linux-x64_bin.deb

By running this command, you will initiate the installation process of the Oracle Java 17 package. The installation may take some time, depending on the package size and your system’s speed. Once the installation is complete, you can verify that Java has been installed successfully by checking the version of Java on your system using the –version command. This will ensure that the installation was successful and that you can confidently proceed with your Java development tasks.

Method 3: Install Java 17 via Archive Method on Ubuntu

The third and highly recommended option for installing Java JDK 17 involves a manual installation process that gives users greater flexibility and control over their Java development environment. This method requires the user to download the archive, extract it, and then manually set up the environment variables.

To start with this manual installation option, visit the downloads page for the latest build version link. Once you have the link, you can use the following curl command to download the archive:

curl  -O https://download.oracle.com/java/17/latest/jdk-17_linux-x64_bin.tar.gz

You may need to install the CURL package if you encounter an error while running this command. You can do this by running the following command in your terminal:

sudo apt install curl

After the archive has finished downloading, you can extract it by running the following command in your terminal:

tar -xvf jdk-17_linux-x64_bin.tar.gz

Next, navigate to the directory where the archive was extracted by using the following command:

cd <directory_name>

As an example, on the machine used for this guide:

cd jdk-17.0.6

Once you are in the appropriate directory, you can run the following commands to install the OpenJDK 17 files:

sudo mkdir -p /usr/local/jdk-17
sudo mv * /usr/local/jdk-17

After installing the OpenJDK 17 files, the next step is to set up the environment variables by adding the following lines to your .bashrc or .bash_profile file:

export JAVA_HOME=/usr/local/jdk-17
export PATH=$JAVA_HOME/bin:$PATH

To apply the changes, you can source the .bashrc or .bash_profile file by running the following command:

source ~/.bashrc

Finally, you can verify that the installation was successful by running the following commands:

java --version
echo $JAVA_HOME

If the installation was successful, you should see an output confirming the version of Java and the installation location.

echo $JAVA_HOME
java 17.0.6 2023-01-17 LTS
Java(TM) SE Runtime Environment (build 17.0.6+9-LTS-190)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.6+9-LTS-190, mixed mode, sharing)
/usr/local/jdk-17

This manual installation option gives users the most significant control over their Java development environment, allowing them to customize the installation according to their specific needs and preferences.

Step 3: Create a Hello World Application with Java on Ubuntu (Optional)

After installing Java JDK 17, it is always a good idea to test the installation to ensure everything works correctly. One way to do this is by creating a small program script that utilizes the famous “Hello World” message. This can be done quickly and efficiently on your Ubuntu system.

To create the Java program file, you can use the following command:

nano hello.java

This will open up the nano text editor and allow you to create a new Java file. You can then add the following Java code to the file:

public class hello {
  public static void main(String[] args) {
    System.out.println("G'day from LinuxCapable!");
  }
}

Once you have added the code to the file, you can save it by pressing CTRL+O and exit the text editor by pressing CTRL+X.

The next step is to compile the code using the following command:

javac hello.java

If there are no errors, the compilation process should complete successfully. Finally, you can run the Java code with the following command:

java hello

This will execute the code and display your terminal’s “Hello World!” message. The output should be similar to the following:

Hello World!

This confirms that your Java JDK 17 installation is working correctly, and you can confidently proceed with your Java development tasks.

As a second application creation test, you can modify the Java code by changing the output message and recompiling it using the javac command. For example, you can adjust the Java code to the following:

public class hello {
  public static void main(String[] args) {
    System.out.println("Welcome to Java Programming!");
  }
}

Once you have made the changes to the Java code, you can save the file and recompile it using the javac command. Finally, you can execute the code using the jaJavaommand, and the output should be:

Welcome to Java Programming!

By testing your Java installation with different Java programs and modifying the code, you can ensure that your Java development environment is working correctly and that you are prepared for your Java development tasks.

Conclusion: Installing Oracle Java 17 on Ubuntu

In conclusion, installing Java JDK 17 on your Ubuntu system is a straightforward process, and several different installation methods are available to suit your preferences and specific requirements, whether you choose to install Java using the ppa repository, the Debian package, or a manual installation you should be able to install Java 17 on your system.