Running a Java program requires a runtime, while building one also requires a compiler. Debian provides OpenJDK packages through APT, including a runtime and a development kit.
This article shows how to check which Java packages your Debian repositories offer and choose the JDK or runtime package to install.
TL;DR
Install Debian’s default Java development kit with APT, or choose a version-specific package after checking that your Debian release provides it.
- Use default-jdk to compile and run Java programs.
- Use default-jre when you only need to run Java applications.
- Check the installed runtime with java --version and the compiler with javac --version.
What Java packages does Debian provide?
Debian provides OpenJDK packages through APT, its package manager. The default-jdk and default-jre packages are meta-packages: they select Debian’s recommended development kit or runtime for that release and architecture.
The JDK adds the compiler and development tools needed to build Java programs. Debian also provides a runtime-only package for applications that do not need compilation.
Debian release and architecture affect which versions the default package selects. For example, Debian’s trixie package details identify its default JDK dependency as OpenJDK 21, while other releases can differ. If a project requires a particular version, query APT for that exact package before installing it.
How to install Java on Debian step by step
Use Debian’s repositories first, then verify that both the launcher and any required compiler are available. Run package installation commands on your Debian machine with an account authorized to install software.
Step 1: Refresh APT’s package index
Before you search for a package, refresh APT’s local index of package metadata. The search then reflects the repositories currently configured on the machine.
sudo apt update
This updates package metadata without upgrading installed packages. Review repository errors before relying on a package search.
Step 2: Check which package version is available
Search APT’s configured sources for the default JDK or a named OpenJDK version. This is especially useful when a project asks for Java 17 or 21 and you do not know whether that package exists for your release.
Run this read-only query for the default package or a version required by your project.
apt-cache policy default-jdk openjdk-21-jdk
APT prints a Candidate field when configured sources offer a package. A value of (none) means you should confirm the Debian release and package name before changing repositories.
Step 3: Install the JDK or runtime
Install the meta-package for Debian’s default, or replace it with the exact package name found in the previous step. A named OpenJDK package follows the form openjdk-21-jdk when that release provides it.
sudo apt install default-jdk
For a machine that only runs Java applications, default-jre avoids installing the compiler and development tools. Check APT’s proposed dependency changes before you confirm the installation.
Step 4: Verify Java and the compiler
Check the runtime launcher and compiler separately. This distinguishes a runtime-only installation from a development kit.
java --version
javac --version
Both commands should print version information when the JDK is installed. If the launcher works but the compiler is missing, install the development package.
Check Java versions, select a default, or remove it
APT controls installed package versions, while Debian’s alternatives system chooses which registered executable runs when you type a shared command such as java. Use alternatives only when multiple Java installations are present and you need to change the selected one.
sudo update-alternatives --config java
Select the entry matching the required Java version. Check the result with java --version. Some builds also invoke javac, so inspect its selected version too.
Set JAVA_HOME only when a build tool or application expects it. It should point to the JDK directory, not the executable.
Debian installations commonly use /usr/lib/jvm. Inspect the active executable to identify its directory.
printf '%s\n' "$JAVA_HOME"
readlink -f "$(command -v java)"
The first command prints the variable if it is set. The second resolves the active Java executable and helps identify its installation directory. For persistent shell configuration, see how to set JAVA_HOME in Linux.
Remove only the package you installed, using its exact package name. APT may leave automatically installed dependencies until they are no longer needed.
sudo apt remove openjdk-21-jdk
sudo apt autoremove
Replace openjdk-21-jdk with the package shown by dpkg-query -W ‘openjdk*’. Review APT’s proposed removals before confirming. Other applications on the machine may depend on Java.
If Java is part of a build workflow, the LinuxForDevices OpenJDK guide covers broader distribution choices. Check the documented Java support route for your Debian release before choosing a version.
Common Java installation errors on Debian
APT errors usually point to repository configuration or package availability. Use the symptom to decide what to inspect next.
- Candidate is (none): confirm the Debian release and package name.
- Unable to locate package: refresh the package index and inspect enabled sources.
- Dependency errors: review APT’s proposed changes before confirming.
Conclusion: use Debian’s package choice
For a standard Debian setup, check the repository candidate first, then verify the launcher and compiler after installation.
Debian’s Java FAQ explains its package recommendations, and the Oracle JDK installation guide documents Oracle’s Debian package route.
FAQ
These answers cover the version and package questions that commonly change the installation command.
How do I install Java 21 on Debian?
Check whether your Debian repositories provide openjdk-21-jdk with apt-cache policy openjdk-21-jdk, then install it with sudo apt install openjdk-21-jdk if a candidate is available.
Do I need the JDK or JRE?
Install the JDK when you need to compile Java code or use development tools. Install the runtime package when you only need to run Java applications.
How can I check the installed Java version?
Run java --version to check the runtime and javac --version to check the compiler.
