How to install Java JDK 21 or OpenJDK 21 on Debian 12

Estimated reading: 3 minutes 405 views

Java is an open-source, and high-level programming language known to be reliable, robust, and portable. It was originally created by Sun Microsystems in the 1990s but is now being owned and maintained by Oracle Corporation. Java is one of the widely adopted programming languages in banking, governments, schools, and powering all types of applications worldwide.

Java Development Kit (JDK) can be described as a set of tools and libraries created by Oracle and other community contributors to enable you to quickly develop Java applications. JDK includes Java Runtime Environment (JRE), a core engine for running Java applications, and extras such as debugger, compiler, and utilities that will increase your efficiency when programming.

In this tutorial, we will consider the use of OpenJDK and the official Oracle JDK. When choosing between the two, factors like support. OpenJDK is a common choice since it’s open-source and ticks all the boxes in the Java SE specification.

Install Oracle JDK 21

The Oracle JDK is not a fully open-source implementation of Java SE specification. It is packaged with additional tools and features that are not available in the standard Java APIs. If your application needs support from Oracle on updates and security patches promptly, then this is a better option for you.

Oracle JDK 21 can be downloaded from Oracle Downloads pages.

wget https://download.oracle.com/java/21/latest/jdk-21_linux-x64_bin.deb

After downloading the package, perform the installation using the package management tool by running the following command:

dpkg -i jdk-21_linux-x64_bin.deb

Check the current default version of Java. If no old version it should show the one we just installed.

java --version

The update-alternatives command is used to update the default version of Java in systems with multiple versions.

To list all installed versions of Java run:

update-alternatives --list java

Install OpenJDK 21

This is a manual process that involves archive download, extracting, and copying the files to a system’s directory.

Go to the OpenJDK 21 release website and download a version suitable for your CPU architecture.

wget https://download.java.net/java/GA/jdk21.0.2/f2283984656d49d69e91c558476027ac/13/GPL/openjdk-21.0.2_linux-x64_bin.tar.gz

We can extract the file downloaded to get all files and directories containing OpenJDK.

tar xvf openjdk-21.0.2_linux-x64_bin.tar.gz

Move the created folder into /usr/local/ directory.

mv jdk-21.0.2/ /usr/local/jdk-21

To set the Java environment, create a new source script.

tee -a /etc/profile.d/jdk21.sh<<EOF
> export JAVA_HOME=/usr/local/jdk-21
> export PATH=$PATH:$JAVA_HOME/bin
> EOF

Source to effect the change in your current shell session.

source /etc/profile.d/jdk21.sh

Check the Java version to verify it works.

java --version

Conclusion

Choosing between OpenJDK and Oracle JDK depends heavily on your application needs and your long-term plans. In the ideal general use case, OpenJDK is an easy choice because it is 100% open-source. But with Oracle JDK you can still use free parts for small projects without licensing.

Hopefully, this article will be useful for you. Good luck!