Java is an essential programming language used for developing various applications like android apps, games, web apps and more. However, running outdated Java versions can cause compatibility issues and security vulnerabilities. So it‘s important to keep Java up-to-date on your Ubuntu system.
In this comprehensive guide, you‘ll learn multiple methods to check the installed Java version on Ubuntu 22.04 and also update it to the latest available version.
Why Keep Java Up-to-Date?
Here are some key reasons why you should keep Java updated on your Ubuntu desktop or server:
-
Security Vulnerabilities: Outdated Java versions contain security holes that can be exploited by hackers. Upgrading Java closes these loopholes.
-
Bug Fixes: Newer Java versions fix bugs and issues present in older releases. Keeping Java updated ensures less crashes.
-
Performance Improvements: Updates also bring performance enhancements that result in faster execution.
-
Compatibility: New Java releases add support for latest technologies and standards. An outdated Java version may not work properly with newer frameworks and libraries.
-
New Features: Major Java versions introduce new capabilities that enable developing richer applications. To leverage those capabilities, you need to update Java.
In short, having the latest Java version results in secure, faster and compatible applications.
Check Installed Java Version on Ubuntu 22.04
Several methods are available to check the Java version installed on your Ubuntu 22.04 system.
Method 1: java -version Command
The easiest way is to use the java -version command in the terminal:
java -version
This prints details about the Java Runtime Environment (JRE) installed on your system:

In the output, focus on these details:
- java version: Shows the Java version number installed. Here it‘s 17.0.1.
- Runtime Environment (build ***): Gives technical details about the JRE.
- Java HotSpot: The name of the Java Virtual Machine (JVM).
This confirms that Oracle Java 17 is installed on this system.
Note: If Java is not installed, you‘ll see an error like "java: command not found".
Method 2: /usr/bin/java -version
Sometimes multiple Java versions can be installed on a system. The java command may represent an older Java version.
To be fully sure of the default Java runtime, use the full path:
/usr/bin/java -version
This will display details about the default Java version linked to the java command.
Method 3: update-alternatives –config java
On Ubuntu and other Debian-based Linux distributions, the update-alternatives system is used to manage multiple installations of an application.
To check Java versions managed through update-alternatives, use:
sudo update-alternatives --config java
Here‘s a sample output:

This shows two Java versions installed – Oracle Java 17 (priority 1081) and OpenJDK Java 11 (priority 1081).
The output marks the currently active Java alternative with an asterisk (*). So in this case, Java 17 is enabled.
Check Installed Java JDK Version
We checked the Java Runtime Environment (JRE) version in the previous section. The JRE allows running Java programs.
For developing Java applications, you need the Java Development Kit (JDK) which bundles extra tools like the Java compiler javac.
Here are a few tips to verify the JDK version on Ubuntu.
Use javac -version
The javac command is part of the JDK for compiling Java source code.
Check its version with:
javac -version
This confirms if JDK is installed and displays the version number:

If JDK is missing, you‘ll see "javac: command not found".
Find javac Path
You can also locate the full path of the javac program using the which command:
which javac
This returns the javac location if JDK is available:
/usr/bin/javac
Search JDK Files on System
The JDK gets installed to a directory like /usr/lib/jvm/java-***/.
You can search for that directory to confirm the JDK:
ls /usr/lib/jvm

This reveals available JDK installations.
Install Java Runtime on Ubuntu 22.04
If Java isn‘t installed on your Ubuntu system, use the following steps to set it up:
-
First, update the package index:
sudo apt update -
Then install the Java Runtime Environment using:
sudo apt install default-jre -
Finally, verify the installation:
java -version
The standard OpenJDK Java Runtime will get downloaded and set up through these commands.

Similarly, utilize sudo apt install default-jdk for setting up the Java Development Kit.
Update Java to Latest Version on Ubuntu
Over time, newer versions and updates for Java come out. Updating to the latest available Java release ensures you get all the benefits mentioned earlier.
Here is the standard process to update Java on Ubuntu:
Step 1: Check Available Updates
First, verify if updates are available for installed Java packages:
sudo apt update
sudo apt list --upgradable
Step 2: Upgrade Java Packages
If updates are shown, upgrade Java by installing them:
sudo apt upgrade
Or target only the Java packages:
sudo apt upgrade <java-package-name>

This will update the JRE/JDK packages to the latest release provided in Ubuntu‘s repositories.
Note: At times, Ubuntu repositories contain older Java versions. For newer ones, consider installing Oracle Java or using a PPA.
Step 3: Verify Updated Version
Finally, check if the Java upgrade was successful:
java -version
The output should display the newer Java version.
And that‘s it! Using apt upgrade keeps Java up-to-date on Ubuntu. Next let‘s see how to install non-repo versions.
Install Oracle Java on Ubuntu
While OpenJDK Java is open-source, Oracle Java is the official proprietary release by Oracle Corporation.
At times, you may need to install Oracle Java due to compatibility or support requirements.
Here is the step-by-step process to set up the latest Oracle Java on Ubuntu:
Step 1: Add Repository
Oracle provides Ubuntu packages on their website, but doesn‘t maintain proper distro repositories.
So first we need to manually add the repository by creating this source file:
sudo nano /etc/apt/sources.list.d/oracle-java.list
And add the following line:
deb http://ppa.launchpad.net/linuxuprising/java/ubuntu jammy main
Save and close the file after adding this line.
Step 2: Import GPG key
Next import the repository signing key to ensure authentic packages:
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3B4FE6ACC0B21F32
Step 3: Install Java Runtime
With the repository enabled, install the latest Oracle Java runtime:
sudo apt update
sudo apt install oracle-java17-installer
Accept the license agreement during the installation process.
Step 4: Set Default Java Version
The installer creates a new Java path like /usr/lib/jvm/java-17-oracle.
If this isn‘t automatically set as the default, use this command:
sudo update-alternatives --set java /usr/lib/jvm/java-17-oracle/bin/java
Step 5: Verify
Finally, check if the new Oracle Java runtime is active:
java -version
The output should mention ‘Java(TM) SE Runtime Environment‘.
And that‘s it! You have installed the latest Oracle Java on Ubuntu 22.04 through these straightforward steps.
Install Java from PPA
PPA or Personal Package Archive allows installing newer software versions than the official Ubuntu repositories.
A few Java PPAs exist with cutting edge releases. You can set up Java from them using:
Step 1: Add Java PPA
Here I‘m using the WebUpd8 PPA which contains multiple recent Java versions:
sudo add-apt-repository ppa:webupd8team/java
Step 2: Update Package List
Next, refresh your package list after adding this new repository source:
sudo apt update
Step 3: Install Java
Now search for available Java packages:
apt-cache search oracle-java
And install your desired version, like Oracle Java 17:
sudo apt install oracle-java17-installer
The installer will setup the Java environment.
Step 4: Make Default (Optional)
Switch to the new Java install using update-alternatives, if needed.
Step 5: Verify
Finally, test by checking the Java version to confirm the installation:
java -version
Through this easy process, you can setup newer Java releases using PPAs on Ubuntu 22.04 or 20.04.
Switch Between Multiple Java Versions
We already covered the update-alternatives system to change the active java command earlier.
Here is the standard syntax to switch Java versions:
sudo update-alternatives --config java
The different installed Java paths will be shown as choices for selection.

Enter the number against the desired Java version you want to enable.
For example, input 1 will select Java 17 in the screenshot above. This will set it as the default java runtime.
Note: You‘ll need root privileges to change system-wide Java versions.
Then verify by checking java -version after the switch.
This update-alternatives method lets you easily change between multiple Java installations.
Uninstall Java from Ubuntu
You may need to completely uninstall Java from Ubuntu if facing issues or for testing environments.
Here is the standard process to remove Java JRE/JDK:
Step 1: Find Package Name
List installed Java packages with:
dpkg --list | grep jdk
And:
dpkg --list | grep jre
Note down the name of packages you want to remove.
Step 2: Uninstall Java Packages
Now uninstall the Java packages.
For example, to remove the OpenJDK JRE and JDK packages:
sudo apt remove openjdk-17-jre-headless openjdk-17-jdk-headless

Type Y when prompted to confirm uninstalling Java packages.
Step 3: Remove configuration (Optional)
You can also delete Java system configs:
sudo update-alternatives --remove-all java
sudo update-alternatives --remove-all javac
And that‘s it! By following this tutorial, you can check java versions, install latest updates, setup Oracle Java, switch between multiple versions and also completely uninstall Java on Ubuntu 22.04.


