How to Install Java on Ubuntu 26.04 and 24.04

Share

Install Java on Ubuntu 26.04 or 24.04 with APT: refresh the package list, then install default-jdk. OpenJDK provides the Java runtime for running Java applications and the tools for compiling your own programs.

This guide explains which version to choose, how to check what is installed, and how to update, upgrade or switch Java without losing your fallback version. If a step fails, leave a comment with the error or contact us.

Before you begin
  • Use Ubuntu 26.04 or 24.04 Desktop or Server, an internet connection and an account with sudo access. Commands assume Bash.
  • Check your application’s required Java version first. A newer major version is not automatically compatible with every application.
  • JDK: includes the runtime and development tools such as javac. JRE: runs applications without the compiler. Choose the JDK if you are learning or developing Java.
  • APT manages the Ubuntu packages in this guide. An IDE, container or version manager may use a separate Java installation.

1. Check your Ubuntu and Java versions

Step 1: Identify the Ubuntu release

Open a terminal. On Ubuntu Desktop, Ctrl+Alt+T normally opens one. Read the release information:

Ubuntu terminal
cat /etc/os-release
dpkg --print-architecture

Look for VERSION_ID="26.04" or "24.04". Architecture matters when interpreting JVM paths: amd64 and arm64 have different directory names.

Step 2: Check the runtime, compiler and selected paths

Run these commands individually. If Java is absent, a command-not-found message is expected; continue to the installation section.

Ubuntu terminal
java --version
javac --version
command -v java
command -v javac

java runs programs; javac compiles source files. Their major versions should match for this walkthrough. Java 21 output begins with openjdk 21; Java 25 begins with openjdk 25. The exact patch and build numbers change with updates. The older spelling java -version also works, including with Java 8.

2. Choose the Java version you need

Use Ubuntu’s default JDK when no application specifies a version. Use an explicitly numbered package when a project requires that major release.

Your situation Package to choose What to expect
Ubuntu 26.04, no specific requirement default-jdk Installs the distribution default, OpenJDK 25.
Ubuntu 24.04, no specific requirement default-jdk Installs the distribution default, OpenJDK 21.
Application requires Java 25 openjdk-25-jdk Available for both releases. On 24.04 it comes from Universe in the updates archive.
Application requires Java 21 or 17 openjdk-21-jdk or openjdk-17-jdk Install the required major release rather than changing the application blindly.
Run applications only default-jre Runtime without the compiler. Use a numbered JRE if the app specifies a version.
Server without graphical Java apps default-jdk-headless or default-jre-headless Omits dependencies needed for graphical Java applications.

The defaults are documented in Ubuntu’s 26.04 package listing and 24.04 package listing. For other major releases, consult the official Java availability table and check your local APT candidate. Do not add another Ubuntu release’s repositories to obtain a newer Java package.

3. Install Java with APT

Step 1: Refresh the package list

This downloads current package metadata; it does not upgrade every installed package.

Ubuntu terminal
sudo apt update

Resolve repository errors before continuing. A full system upgrade is not a prerequisite for installing Java.

Step 2: Install the default JDK

For a general Java setup, run:

Ubuntu terminal
sudo apt install default-jdk

Read APT’s proposed changes before confirming. If the current package is already installed, APT reports that. You do not need to install a separate JRE after installing the JDK.

Alternative: install a required major version

Skip this if the default JDK meets your needs. Check the candidate before installing Java 25:

Ubuntu terminal
apt-cache policy openjdk-25-jdk
sudo apt install openjdk-25-jdk

A candidate of (none) means the configured repositories do not provide an installable version. Follow troubleshooting below before running the installation command. For an application requiring Java 21 or 17, substitute openjdk-21-jdk or openjdk-17-jdk. Choose one required package, rather than installing every version.

Step 3: Verify the result

Check both tools again:

Ubuntu terminal
java --version
javac --version

If another Java version was installed previously, the newly installed package may not become the selected default. Use the switching section below. Installing a package and selecting it are separate actions.

4. Compile and run a small Java program

Step 1: Create a temporary practice directory

The following commands create a unique directory and a new source file, avoiding an overwrite of an existing project.

Ubuntu terminal
java_demo_dir=$(mktemp -d "$HOME/java-demo.XXXXXX")
cd "$java_demo_dir"
cat > HelloUbuntu.java <<'EOF'
public class HelloUbuntu {
    public static void main(String[] args) {
        System.out.println("Java works on Ubuntu!");
    }
}
EOF

The filename must match the public class name, including capitalization: HelloUbuntu.java.

Step 2: Compile, then run

Compile the source into a class file, then run that class from the current directory.

Ubuntu terminal
javac HelloUbuntu.java
java -cp . HelloUbuntu

Compilation normally finishes without output. The second command should print the line below. Use the class name without .class.

Java works on Ubuntu!

This checks both compilation and execution. It does not prove that a larger application, its dependencies or its build plugins support your chosen Java version.

5. List installed Java versions and switch the default

Step 1: List registered installations

Ubuntu’s Java helper lists package-managed installations, their priorities and their JVM directories.

Ubuntu terminal
update-java-alternatives --list

Copy the installation name from the first column and note its full directory from the last column. A manually extracted JDK or a version-manager installation may not appear. The Ubuntu manual documents how this helper switches the registered tool set.

Step 2: Select the whole JDK

Replace the placeholder with an exact first-column name from your own list. Do not paste the placeholder literally.

Ubuntu terminal
sudo update-java-alternatives --set NAME_FROM_FIRST_COLUMN
hash -r
java --version
javac --version

This changes registered Java tools system-wide and may affect other users or applications that use those defaults. Choose a maintenance window on shared servers. An already running JVM continues using the version with which it started.

Alternative: select runtime and compiler separately

If you use the interactive alternatives menus, select matching major versions in both menus.

Ubuntu terminal
sudo update-alternatives --config java
sudo update-alternatives --config javac

Changing only java can leave javac on a different version. A PATH entry from SDKMAN or another manager can override these selections; check the resolved executable if the result looks wrong.

6. Set JAVA_HOME only when a tool requires it

JAVA_HOME points to the JDK directory, not its bin subdirectory and not the java executable. Ordinary Java commands can work without it. Set it when your build tool or application asks for it.

Step 1: Derive the active compiler’s JDK directory

First select the intended JDK. This avoids hardcoding an architecture-specific path.

Ubuntu terminal
export JAVA_HOME="$(dirname "$(dirname "$(readlink -f "$(command -v javac)")")")"
printf '%s\n' "$JAVA_HOME"
"$JAVA_HOME/bin/java" --version

For an Ubuntu APT installation, expect a directory under /usr/lib/jvm/. If the compiler is missing or a version-manager shim resolves outside the actual JDK, use the JVM directory shown by update-java-alternatives --list instead. Check it before saving anything.

Step 2: Make it persistent for your Bash login

Open your own profile. Do not overwrite the file or append duplicate definitions.

Ubuntu terminal
nano "$HOME/.profile"

If nano is not installed, use another text editor. Add or update a single export JAVA_HOME="..." line using the verified JDK directory from Step 1. Save, then sign out and back in. Check printenv JAVA_HOME and "$JAVA_HOME/bin/java" --version in the new session. Update this setting if you later change JDKs. A systemd service needs its own service environment; your shell profile does not configure it.

To undo this setting, remove only your Java export line from ~/.profile and start a new login session. An IDE may have a separate project JDK setting even when your terminal is correct.

7. Update Java within the installed major version

A routine package update applies available fixes to an installed major release, for example from one Java 21 patch to a newer Java 21 patch. It does not automatically migrate an explicitly installed openjdk-21-jdk package to Java 25.

Step 1: Check the installed and available package versions

This example is for Java 21. Replace both package names with openjdk-25-jdk and openjdk-25-jre-headless if you installed Java 25.

Ubuntu terminal
sudo apt update
apt-cache policy openjdk-21-jdk openjdk-21-jre-headless

Compare Installed and Candidate. The runtime implementation lives in the headless package even when you installed the full graphical JDK.

Step 2: Apply available Ubuntu updates

The normal system-update route updates Java together with other installed packages.

Ubuntu terminal
sudo apt upgrade

This can upgrade other system packages as well. Review the proposed changes and any restart prompt. If you need a targeted Java 21 update instead, use the next command.

Alternative: target the installed Java packages

Run this only for the major release you actually installed. Dependencies may be updated too.

Ubuntu terminal
sudo apt install --only-upgrade openjdk-21-jdk openjdk-21-jdk-headless openjdk-21-jre openjdk-21-jre-headless

For Java 25, replace 21 with 25 in all four package names. A headless-only or JRE-only installation needs only its installed packages. APT may skip names that are not installed; review the result. The APT manual explains the upgrade-only option.

Step 3: Restart affected applications and verify

New launches use updated files. Existing Java processes need a restart to load the updated runtime.

Ubuntu terminal
java --version
javac --version

Close and reopen desktop applications. For a server, restart the specific Java service during a maintenance window and check its logs and health. Do not restart unrelated services or assume a terminal version check proves the running server has updated.

8. Upgrade from Java 21 to Java 25, with a fallback

Step 1: Check compatibility and keep Java 21

Confirm your application, framework and build tool support Java 25. Back up the project configuration or use version control. Record the current JDK name and JAVA_HOME.

Ubuntu terminal
java --version
javac --version
printenv JAVA_HOME
update-java-alternatives --list

Keep Java 21 installed until the application works on Java 25. A major upgrade can change defaults during installation, so plan downtime if services depend on the system selection.

Step 2: Install Java 25 alongside Java 21

Check the candidate, then install the numbered JDK package.

Ubuntu terminal
sudo apt update
apt-cache policy openjdk-25-jdk
sudo apt install openjdk-25-jdk
update-java-alternatives --list

Both supported Ubuntu releases provide Java 25 packages. If the candidate is missing, resolve the repository configuration below. You do not need to upgrade Ubuntu 24.04 itself just to install Java 25.

Step 3: Select and test Java 25

Use the exact Java 25 installation name from the list.

Ubuntu terminal
sudo update-java-alternatives --set JAVA_25_NAME_FROM_LIST
hash -r
java --version
javac --version

Update JAVA_HOME and any IDE or service configuration that still points at Java 21. Run the compile example again, then run your project’s tests and start the application. A system-default change does not override an explicitly configured JDK path.

Step 4: Roll back if the application fails

Select the old Java 21 installation using its exact name from the list.

Ubuntu terminal
sudo update-java-alternatives --set JAVA_21_NAME_FROM_LIST
hash -r
java --version
javac --version

Restore the previous JAVA_HOME, project JDK and service configuration, then restart the affected application. Keep the failing version installed while investigating. The same switch-back approach applies when moving between other installed major versions.

9. Fix common Java installation problems

Issue 1: Java works, but javac is missing

You may have installed only a JRE, or your PATH may select a different installation. Install the JDK required by the application, then check both paths.

Ubuntu terminal
sudo apt install default-jdk
command -v java
command -v javac
java --version
javac --version
Issue 2: Unable to locate package, or no installation candidate

First refresh APT and check the Ubuntu release and configured sources. On Ubuntu 24.04, Java 25 needs Universe and the release’s updates archive. If Universe is disabled, enable it using Ubuntu’s repository helper:

Ubuntu terminal
sudo apt install software-properties-common
sudo add-apt-repository universe
sudo apt update
apt-cache policy openjdk-25-jdk

If no candidate appears, inspect /etc/apt/sources.list.d/ubuntu.sources, any other .sources or .list files, and /etc/apt/sources.list if present. Use only suites for your own release: noble on 24.04 or resolute on 26.04, with the matching updates and security suites. Fix failed mirrors or disabled suites without replacing the whole file blindly.

Issue 3: The wrong Java version still runs

Find every matching executable, resolve the first one and compare the environment setting:

Ubuntu terminal
type -a java javac
readlink -f "$(command -v java)"
readlink -f "$(command -v javac)"
printenv JAVA_HOME

An earlier PATH entry, shell alias, IDE configuration or version manager can take priority over Ubuntu alternatives. Correct that setting and start a fresh terminal. Changing JAVA_HOME alone does not change which java your shell finds.

Issue 4: UnsupportedClassVersionError

The runtime is older than the version used to compile the class. Run the program with a compatible newer runtime, or rebuild it for the required older release. If the code and dependencies support Java 21, a JDK 25 compiler can target it with:

Ubuntu terminal
javac --release 21 HelloUbuntu.java

The --release option checks the target platform APIs as well as the class-file version. It cannot make newer language features or incompatible third-party dependencies work on an older runtime. Configure the equivalent target in Maven or Gradle for a full project.

Issue 5: Could not find or load main class

For the practice program, run from the directory containing HelloUbuntu.class and use java -cp . HelloUbuntu. Check capitalization. Packaged projects need their proper classpath or build-tool command. For a JAR application, follow its own launch instructions; do not assume every JAR is executable.

10. Remove a Java version safely

Step 1: Select a replacement before removal

List installed OpenJDK packages and registered installations.

Ubuntu terminal
dpkg -l 'openjdk-*'
update-java-alternatives --list

Switch applications and alternatives to a version you are keeping. Update JAVA_HOME and service or IDE paths. Removing the only runtime prevents applications that depend on it from starting.

Step 2: Preview removal, then remove the chosen major

This example removes Java 21. Substitute the major version you intend to remove.

Ubuntu terminal
sudo apt -s remove openjdk-21-jdk openjdk-21-jdk-headless openjdk-21-jre openjdk-21-jre-headless

Read the simulation first. APT may propose removing default-jdk, default-jre or dependent applications too. If the plan is acceptable, repeat the command without -s. Avoid wildcards and an unreviewed autoremove. Do not delete /usr/lib/jvm manually; it can contain versions you still need.

Removing only default-jdk removes a metapackage and may leave the real JDK installed. Check java --version, javac --version and the installed-package list afterwards. Your source files and the practice directory are separate from the Java packages.

Helpful resources

Official documentation

Frequently asked questions

Do I need the JDK or just the JRE?

Install the JDK to write or compile Java programs. It includes a runtime. A JRE is enough to run an application when its documentation does not require development tools. Match the major version required by the application.

Does apt upgrade move Java 21 to Java 25?

It updates installed packages to available candidates. The explicitly numbered openjdk-21-jdk package stays on major version 21. To move to Java 25, install openjdk-25-jdk, select it, update any fixed JDK paths and test the application. A distribution upgrade can change default-jdk dependencies.

Can I keep several Java versions installed?

Yes. Ubuntu can install different numbered OpenJDK packages together. Use update-java-alternatives to select a registered JDK, or configure a specific JDK directory in the application. Keep JAVA_HOME and the application configuration consistent with the chosen version.

Why do java and javac show different versions?

The runtime and compiler can have separate alternatives selections, or your PATH can select tools from different installations. Check both executable paths, select matching tools and review any version manager or IDE configuration.

Do I need to set JAVA_HOME after installing Java?

Usually not for ordinary java and javac commands. Set it when a build tool or application requires it. It must point to the JDK directory, not the bin directory or the java executable, and should be updated when you change JDKs.

Related Ubuntu guides: Install Visual Studio Code | Install Android Studio. Android Studio normally uses its bundled runtime; follow the project’s own Gradle JDK requirements.