Last updated:

Quick answer: run sudo apt update && sudo apt install default-jdk, then check it with java -version and javac -version. The same commands work on Ubuntu 26.04, Ubuntu 24.04, Debian 13 and Debian 12. What differs is the Java version you get.

default-jdk installs OpenJDK, the open-source JDK (Java Development Kit: the compiler, tools and runtime for building Java programs). Installing from the distribution repository means security updates arrive through normal apt upgrade, which is the main reason to prefer it over a manual download.

Which Java version does default-jdk install?

Releasedefault-jdk packageOther JDKs in the repositoryHow checked
Ubuntu 26.04 LTS2:1.25-77openjdk-25 and openjdk-21 source packages (25.0.4.1 and 21.0.12.1 in updates)Launchpad, 26 Oct 2026. The package version points to Java 25; I did not install it on 26.04.
Ubuntu 24.04 LTS2:1.21-75+exp1 (installs OpenJDK 21)openjdk-25-jdk (25.0.4.1+1-1~24.04.4), openjdk-17-jdk (17.0.20.1)Installed and tested on Ubuntu 24.04.5, 26 Oct 2026
Debian 13 (trixie)2:1.21-76openjdk-21-jdk 21.0.12.1, openjdk-25-jdk 25.0.3+9Debian package archive, 26 Oct 2026. Not installed on a Debian host.
Debian 12 (bookworm)2:1.17-74openjdk-17-jdk 17.0.19+10Debian package archive, 26 Oct 2026. Not installed on a Debian host.

The commands are the same on all four. The screenshots below come from the original 2024 version of this article, so your version numbers will differ from them.

Advertisement

Install JDK on Ubuntu and Debian

Step 1: Check whether Java is already installed

Skip this step if you only want the default JDK. Otherwise check what is there first:

java -version

If Java is missing, the shell answers with Command 'java' not found and a list of packages that provide it. You do not need sudo for this command. The screenshot below is from the original 2024 version of this guide.

Terminal showing the java command not found error before JDK is installed

The error means no Java runtime is installed yet, so the next steps install the JDK.

Step 2: Refresh the package lists

Refresh the package index so apt sees the current OpenJDK build instead of a stale one:

sudo apt update
Terminal output of sudo apt-get update on Ubuntu, from the original 2024 guide

Step 3: Install JDK

default-jdk is a metapackage that pulls in the distribution’s default OpenJDK (runtime, javac and the developer tools). It is the right choice unless you need a specific Java version (see the version section below).

sudo apt install default-jdk
Terminal output of sudo apt install default-jdk installing the JDK packages, from the original 2024 guide

Step 4: Verify The Installation

Check both the runtime and the compiler. javac only exists when the JDK (not just the JRE) is installed:

java -version
javac -version

The versions printed should match the table above for your release.

Stuck at this step? Ask us, it is free and include the exact error.

Terminal output of java -version after the JDK installation, from the original 2024 guide

Output from my test on Ubuntu 24.04.5 (26 Oct 2026); the build string differs per release:

openjdk version "21.0.12.1" 2026-08-18
OpenJDK Runtime Environment (build 21.0.12.1+1-1-24.04.4-Ubuntu)
OpenJDK 64-Bit Server VM (build 21.0.12.1+1-1-24.04.4-Ubuntu, mixed mode, sharing)

javac 21.0.12.1

Compile and run a test program

A version string only proves the files exist. Compiling a small program proves the JDK works end to end. Create Hello.java, compile it and run it:

cat > Hello.java <<'EOF'
public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello from Java " + System.getProperty("java.version"));
    }
}
EOF
javac Hello.java
java Hello

Expected output on my Ubuntu 24.04 test machine:

Hello from Java 21.0.12.1

Install a specific Java version instead

If an application needs a particular Java version, install that JDK by package name instead of default-jdk. Check what your release offers first:

apt-cache policy openjdk-21-jdk openjdk-25-jdk openjdk-17-jdk

A package that shows no Candidate is not available on your release (for example openjdk-17-jdk is not in Debian 13). Install the one you need:

sudo apt install openjdk-21-jdk

With several versions installed, choose the active ones:

sudo update-alternatives --config java
sudo update-alternatives --config javac
Advertisement

Set JAVA_HOME

Build tools such as Maven and Gradle often want JAVA_HOME. This prints the path of the JDK that is currently active (on my amd64 test machine it printed /usr/lib/jvm/java-21-openjdk-amd64; on arm64 the directory name ends in arm64):

dirname $(dirname $(readlink -f $(which javac)))

To set it for your user, add this line to ~/.bashrc and open a new shell. It follows whichever JDK update-alternatives selects:

export JAVA_HOME=$(dirname $(dirname $(readlink -f $(which javac))))

Troubleshooting

SymptomCauseFix
E: Unable to locate package default-jdkPackage lists are empty or the universe component is disabledRun sudo apt update. On Ubuntu, sudo add-apt-repository universe if universe is disabled.
javac: command not found but java worksOnly a runtime (JRE) is installed, for example openjdk-21-jre-headlesssudo apt install default-jdk
Picked up JAVA_TOOL_OPTIONS: ... printed on every commandThe JAVA_TOOL_OPTIONS environment variable is set by your shell, profile or CI image. It appeared in my test sandbox and is not an error.echo $JAVA_TOOL_OPTIONS to see it; unset it if you did not intend it.
java -version shows an older version after installing a newer JDKThe alternatives system still points to the old onesudo update-alternatives --config java
UnsupportedClassVersionError (class file version)The class was compiled by a newer JDK than the Java that runs itRun it with the same or newer Java, or compile with javac --release 17 for older targets.

Remove the JDK

sudo apt remove default-jdk removes the metapackage. Run sudo apt autoremove afterwards and read the list it shows before confirming, because it removes every package nothing else needs.

FAQ

JDK or JRE? The JRE only runs Java programs. The JDK includes the JRE plus javac and the other developer tools. Install the JDK if you compile code or run build tools.

Is OpenJDK the same as Oracle JDK? OpenJDK is the open-source reference implementation that Ubuntu and Debian package. Oracle publishes its own builds from the same code base; for most server and development use, the distribution OpenJDK is sufficient.

Advertisement