How to Install Java on Ubuntu 22.04
In this step-by-step tutorial, you will learn how to install Java on Ubuntu 22.04 server in multiple ways, configure the default Java version, and set up the JAVA_HOME environment variable.
What is Java?
Java, released in 1995, is one of the world’s most popular programming languages. It’s widely used to develop high-performance applications such as desktop and Android apps, web applications, games, and more. Java is a high-level, general-purpose, and object-oriented programming language known for its versatility and cross-platform compatibility.
What is Java Used For?
Java’s platform-independent nature makes it ideal for building cross-platform applications. It’s commonly used in game development, web and mobile apps, chatbots, and enterprise-level software, due to its reliability and speed.
Prerequisites
Before starting, ensure you have the following:
- A running instance of Ubuntu 22.04 server.
- SSH access with a sudo-enabled user.
How to Install Java on Ubuntu 22.04 – Step by Step Guide
Follow these steps to install Java on Ubuntu 22.04:
Step 1: Install Java on Ubuntu 22.04
There are two main options for installing Java:
- OpenJDK – Open-source implementation available in Ubuntu’s repository.
- Oracle JDK – Maintained by Oracle, free for commercial use starting from JDK 17 under the NFTC license.
First, check if Java is installed:
java -version
If Java is not installed, proceed with the installation options below.
Option 1: Install Java Using OpenJDK
OpenJDK provides essential Java development tools and libraries. Install OpenJDK 17 with:
sudo apt install openjdk-17-jdk -y
Verify the Java installation:
java -version
Option 2: Install Oracle Java 17 from PPA
To install Oracle JDK 17, add the Linux Uprising PPA:
sudo add-apt-repository ppa:linuxuprising/java
Update the package list and install Oracle Java:
sudo apt update
sudo apt install oracle-java17-installer oracle-java17-set-default

Verify Oracle Java 17 installation:
java -version
Step 2: Configure Default Java Version on Ubuntu
If multiple Java versions are installed, set the default version with:
sudo update-alternatives --config java
Select the version you wish to set as default. Verify the default version:
java -version
Step 3: Configure JAVA_HOME Environment Variable (Recommended)
The JAVA_HOME variable points to the Java installation directory. Set it to help Java-based applications locate the JRE path.
First, determine Java’s installation path:
sudo update-alternatives --config java
Edit the /etc/environment file to add the JAVA_HOME variable:
sudo nano /etc/environment
Add the following line, replacing the path as needed:
JAVA_HOME="/usr/lib/jvm/java-17-openjdk-amd64"
Reload the environment file:
source /etc/environment
Verify JAVA_HOME:
echo $JAVA_HOME
Step 4: Install Multiple Java Versions on Ubuntu 22.04
In real-world environments, you may need multiple Java versions installed simultaneously. For example, one application may require Java 8, while another requires Java 17.
You can safely install multiple versions side by side.
Install Java 8 and Java 11 Along with Java 17
After installation, list all available Java versions:
This allows you to quickly switch Java versions based on application requirements.
Step 5: Switch Java Version for a Specific User (Optional)
If you don’t want to change the system-wide Java version, you can configure Java per user.
Edit the user’s shell configuration file:
Add:
Reload the file:
This approach is useful in shared servers, CI/CD environments, or developer machines.
Step 6: Verify Java Compiler (javac)
Installing Java is not complete unless the Java compiler is available.
Check the compiler version:
If javac is missing, ensure you installed the JDK, not just the JRE.
Correct package example:
Step 7: Test Java with a Sample Program
To confirm Java is working correctly, create a simple Java program.
Create a file:
Add the following content:
Compile the program:
Run it:
If you see the output message, Java is correctly installed and functioning.
You’ve successfully installed and configured Java on Ubuntu 22.04! Now you can start building Java applications and running Java code. If you found this guide helpful, check out our other tutorials on PostgreSQL, MongoDB, and Docker setup on Ubuntu.
