Introduction
Java is one of the most widely used programming languages, renowned for its portability, performance and robust ecosystem. The latest long-term support (LTS) release is Java 17, which introduces new features like sealed classes, pattern matching for switch statements, and low-pause garbage collection.
In this in-depth tutorial, we will walk through the entire process of installing and configuring Java 17 on an Ubuntu system. We will cover:
- Understanding the differences between OpenJDK and Oracle JDK
- Adding the official Oracle PPA repository
- Downloading and manually installing JDK packages
- Setting the preferred Java version using update-alternatives
- Pointing JAVA_HOME to the correct installation directory
- Testing out the installation with a simple Java program
By the end, you will have Java 17 fully up and running, ready to build Java applications on Ubuntu. Let’s get started!
Prerequisites
Before installing Java, your Ubuntu system should have:
- Ubuntu 20.04 or higher for the latest compatibility
- A standard user account with
sudo
privileges - Internet connectivity to download packages
- Familiarity with basic Linux and Bash shell commands
It is also recommended to update your system packages beforehand using:
1sudo apt update && sudo apt upgrade
This will ensure all existing packages are patched to their latest versions.
OpenJDK vs. Oracle JDK
The first decision is whether to install OpenJDK or Oracle’s JDK. Let’s understand their differences:
OpenJDK is the free and open-source implementation of Java SE. It is developed by the OpenJDK community and licensed under GPL. Packages are included in Ubuntu’s main repositories.
Oracle JDK is Oracle’s proprietary, closed-source JDK. It has a more aggressive release cycle but requires accepting Oracle’s licensing terms. It is not in standard Ubuntu repositories.
For personal use and most development purposes, OpenJDK is recommended. But some enterprises may mandate using Oracle JDK. Both are nearly identical in terms of features and performance.
In this guide, we will cover installation methods for both OpenJDK and Oracle JDK.
Installing OpenJDK 17
OpenJDK can be easily installed from the main Ubuntu repositories using the apt
package manager:
1sudo apt update
2sudo apt install openjdk-17-jdk
The openjdk-17-jdk
package contains the full Java Development Kit including the javac compiler, JVM, JRE and all libraries.
To verify, check the version using:
1java -version
This should print details confirming OpenJDK 17 is installed and is the default Java version.
Installing Oracle JDK 17
Oracle JDK is not part of the main Ubuntu repositories, so we need to add Oracle’s PPA repository to install it:
1sudo add-apt-repository ppa:linuxuprising/java
When prompted, press Enter to continue adding the PPA. This contains packages for the latest Oracle JDK releases.
Next, update your package index and install the JDK:
1sudo apt update
2sudo apt install oracle-java17-installer
During installation, you will need to accept the Oracle license agreement.
Once completed, run java -version
to confirm Oracle 17 is installed and set as the default.
Manual Installation
You can also manually download and install Oracle JDK instead of using the PPA method:
Download the compressed JDK archive from Oracle’s website. Make sure to grab the Linux x64 version.
Use
wget
to download the.tar.gz
file or copy the link address and download with your browser.Create a directory to extract the JDK into, for example
/opt/jdk
:1sudo mkdir /opt/jdk
Extract the archive contents into the directory:
1sudo tar xzf /path/to/jdk-17_linux-x64_bin.tar.gz -C /opt/jdk
Remove the downloaded
.tar.gz
file if no longer needed.
The JDK is now installed, but we still need to set it as the default Java version.
Setting the Default Java Version
If you have multiple Java installations present, you can configure which one is used system-wide with update-alternatives
:
1sudo update-alternatives --config java
This will display all JDKs detected on your system. Enter the number for the desired default version.
For example, to use Oracle’s JDK 17 as the system default, select the corresponding entry from the list.
Now when you run java -version
, it should show the version you configured as default.
Setting JAVA_HOME Environment Variable
It is highly recommended to set the JAVA_HOME
environment variable, which points to the base installation directory of your desired Java version.
Many Java-based applications rely on this variable to determine where Java is installed.
First, identify the install location of the JDK you want to use. For OpenJDK on Ubuntu it is usually /usr/lib/jvm/java-17-openjdk-amd64
.
Then open /etc/environment
using sudo or root access and add the path:
1sudo nano /etc/environment
JAVA_HOME="/usr/lib/jvm/java-17-openjdk-amd64"
Save and close the file, then reload it:
1source /etc/environment
Finally, echo the variable to verify it is set:
1echo $JAVA_HOME
This should print the path you specified.
Testing the Java Installation
As a quick test to validate everything is working, we can write a simple “Hello World” Java program:
1public class HelloWorld {
2
3 public static void main(String[] args) {
4 System.out.println("Hello, World!");
5 }
6
7}
Save this as HelloWorld.java
, then compile and run:
1javac HelloWorld.java
2java HelloWorld
If you see the “Hello World!” message printed, Java is installed and working properly!
Conclusion
In this comprehensive guide, we covered various methods to install Java 17 on Ubuntu, configure the default runtime version, set JAVA_HOME and test the installation.
You are now ready to start developing Java applications on Ubuntu. The steps can also be adapted to install older or newer versions of Java.