Java is a popular programming language used to develop mobile and desktop applications. Java does not come pre-installed on Ubuntu. However, you can install Java on Ubuntu Linux manually through the default repository or directly from the Oracle platform. If you also work with other languages, see how to install Go on Ubuntu 24.04. Java has three core components: JDK (Java Development Kit), JRE (Java Runtime Environment), and JVM (Java Virtual Machine). JDK is used to develop applications, JRE runs Java-based applications, and JVM executes Java programs.

Quick answer

The easiest way to install Java on Ubuntu 24.04 is through Ubuntu’s default repository, usually with OpenJDK. If a project needs Oracle Java or a specific Java release, install that version and then set the default with update-alternatives.

JDK or JRE: which do you need?

Installing the wrong one is the most common early mistake. The JRE only runs Java programs, while the JDK adds the compiler and developer tooling on top of it:

You want to Install Package
Run a Java app or .jar file JRE default-jre
Write and compile Java code JDK default-jdk
Build with Maven or Gradle JDK default-jdk

When in doubt install the JDK. It contains the JRE, so nothing is lost by choosing it.

How to Install Java on Ubuntu 24.04

Method Source Auto-updates Best for
APT (default-jdk) Ubuntu repository Yes (apt upgrade) Most users
Oracle deb package oracle.com No Specific Oracle JDK version
App Center Oracle deb via GUI No GUI-preferred install

Method 1: Install Java on Ubuntu from the Default APT Repository

Ubuntu includes Java in its APT repository, which makes installation straightforward and guarantees a stable version that is easy to upgrade. To start writing and running Java code, you can also install Visual Studio Code on Ubuntu 24.04 as your editor.

Step 1: Update System Packages

sudo apt update && sudo apt upgrade
Terminal output of sudo apt update and upgrade on Ubuntu 24.04

Step 2: Install Java (JDK)

Install the default JDK, which includes JRE and JVM:

sudo apt install -y default-jdk
Terminal output of sudo apt install default-jdk on Ubuntu 24.04

To install only JRE (without developer tools):

sudo apt install -y default-jre
Terminal output of sudo apt install default-jre on Ubuntu 24.04

Step 3: Verify the Java Version

java --version
Terminal output of java --version showing Java version on Ubuntu 24.04

Check the Java compiler version:

javac --version
Terminal output of javac --version showing Java compiler version on Ubuntu 24.04

If java responds but javac reports command not found, you installed the JRE rather than the JDK. Install default-jdk to add the compiler.

Method 2: Install Java on Ubuntu Using the Oracle Deb Package

Download the Oracle JDK deb package directly from Oracle’s website to get a specific JDK version or the latest Oracle Java release.

Step 1: Download the Java Deb Package

Browse the official Java website, locate the Linux x64 Debian Package, and download it with wget:

wget https://download.oracle.com/java/21/archive/jdk-21.0.2_linux-x64_bin.deb
Terminal output of wget downloading Oracle JDK 21 deb on Ubuntu 24.04

Step 2: Install the Oracle JDK

sudo apt install ./jdk-21.0.2_linux-x64_bin.deb
Terminal output of sudo apt install Oracle JDK 21 deb on Ubuntu 24.04

Note: If you downloaded the deb package from a browser, it will be in your Downloads folder. Install it with:

sudo apt install ./Downloads/jdk-21.0.2_linux-x64_bin.deb

Oracle JDK installs to /usr/lib/jvm/ alongside any OpenJDK versions rather than replacing them. Both remain available, which is why the update-alternatives step below matters once you have more than one.

Method 3: Install Java on Ubuntu Using App Center

You can also install Java through App Center using the Oracle deb package without using the terminal.

Step 1: Download the Java Deb Package from the Browser

Navigate to the Java website and download the Linux x64 Debian Package:

Oracle Java website showing Linux x64 Debian Package download link

Step 2: Open the Deb Package with App Center

Open the Downloads folder and locate the JDK deb package:

File manager showing Oracle JDK deb package in Downloads folder on Ubuntu 24.04

Right-click the deb file and select Open With App Center:

File manager context menu showing Open With App Center for JDK deb on Ubuntu 24.04

Click the Install button and enter your credentials when prompted to complete the installation.

How to Install a Specific Java Version on Ubuntu 24.04

To see all available OpenJDK versions in Ubuntu’s repository:

sudo apt search "OpenJDK"
Terminal output of apt search OpenJDK listing available Java versions on Ubuntu 24.04

Install the version you need (replace 21 with your required version number):

sudo apt install -y openjdk-21-jdk
Terminal output of sudo apt install openjdk-21-jdk on Ubuntu 24.04

How to Set the Default Java Version on Ubuntu 24.04

Step 1: Check the Current Default Version

java --version
Terminal output of java --version showing current default Java on Ubuntu 24.04

Step 2: Switch the Default Java Version

Run the following command to list all installed Java versions. Enter the number for the version you want and press Enter:

sudo update-alternatives --config java
Terminal output of sudo update-alternatives --config java on Ubuntu 24.04

That command only switches the runtime. The compiler is tracked separately, so run the matching command for javac or you will end up compiling with one version and running with another:

sudo update-alternatives --config javac

Step 3: Verify the Change

java --version
Terminal output of java --version after changing default Java version on Ubuntu 24.04

How to Set JAVA_HOME on Ubuntu 24.04

Build tools such as Maven, Gradle, and Tomcat read the JAVA_HOME variable rather than searching your PATH. Ubuntu does not set it for you, so it is a frequent source of build failures on a fresh install.

First find the real path of the active Java installation:

readlink -f $(which java)

That returns something like /usr/lib/jvm/java-21-openjdk-amd64/bin/java. Drop the trailing /bin/java to get the value for JAVA_HOME. To set it for your own user, append it to your shell profile:

echo 'export JAVA_HOME=/usr/lib/jvm/java-21-openjdk-amd64' >> ~/.bashrc
echo 'export PATH=$PATH:$JAVA_HOME/bin' >> ~/.bashrc
source ~/.bashrc

Confirm it resolved correctly before running any build:

echo $JAVA_HOME

Test the installation with a small program

Compiling one file is the quickest way to prove the JDK works end to end rather than just reporting a version number. Create the file:

nano Hello.java

Add the following code, then press CTRL+X, Y, and Enter to save:

public class Hello {
    public static void main(String[] args) {
        System.out.println("Java is working on Ubuntu 24.04");
    }
}

Compile and run it. The file name must match the class name, which is the usual cause of a compile error on the first attempt:

javac Hello.java
java Hello

Troubleshooting Java on Ubuntu 24.04

Problem Cause Fix
javac: command not found JRE installed instead of JDK sudo apt install default-jdk
JAVA_HOME is not set Variable never defined Set it in ~/.bashrc as shown above
UnsupportedClassVersionError Compiled on a newer JDK than the runtime Align java and javac with update-alternatives
Wrong version after switching Only java was switched Also run update-alternatives --config javac
Class name error on compile File name does not match class name Save public class Hello as Hello.java

UnsupportedClassVersionError deserves a note because the message is opaque. It means the .class file was produced by a newer compiler than the JVM trying to run it — almost always the result of switching java without switching javac.

How to Uninstall Java from Ubuntu 24.04

What to remove Command
Default JDK and JRE sudo apt remove -y default-jdk
Specific OpenJDK version sudo apt remove -y openjdk-21-jdk
All Java packages sudo apt autoremove java* -y
sudo apt remove -y default-jdk
Terminal output of sudo apt remove default-jdk on Ubuntu 24.04

To remove a specific OpenJDK version:

sudo apt remove -y openjdk-21-jdk
Terminal output of sudo apt remove specific Java version on Ubuntu 24.04

To completely remove all Java packages:

sudo apt autoremove java* -y
Terminal output of sudo apt autoremove java on Ubuntu 24.04

Remember to delete the JAVA_HOME lines from ~/.bashrc afterwards, otherwise the variable keeps pointing at a directory that no longer exists.

Before choosing a Java version

Most Ubuntu users should start with OpenJDK from the default repository because it is simple to update and works for common Java applications, build tools, and development tasks. If a project instead targets the .NET Framework, you can install Mono on Ubuntu 24.04 alongside it.

Use a specific Oracle or OpenJDK version only when an application requires it. After installing more than one Java version, confirm the active version with java -version before troubleshooting the app itself.

Related Ubuntu 24.04 guides