Install Java JDK 21 (OpenJDK 21) on Debian 12.7.0 (64-bit, English)
This guide will take you through the process of installing and using OpenJDK 21 on Debian 12.7.0 (64-bit, English). OpenJDK is the open-source implementation of the Java Development Kit (JDK), which includes the Java Virtual Machine (JVM) and other tools necessary for developing Java applications. By the end of this guide, you will have a working Java development environment using OpenJDK 21.
Installing OpenJDK 21 on Debian 12
To begin, you need to update the package lists for your distribution. Open your terminal and run the following command:
sudo apt update
Next, you can install OpenJDK 21 by running:
sudo apt install openjdk-21-jdk
The installation process will prompt you for confirmation before proceeding. Once the installation is complete, verify that OpenJDK 21 is installed by running:
java --version
If the installation was successful, you should see output similar to the following:
openjdk 21 2023-03-21
OpenJDK Runtime Environment (build 21+36-1738 or later)
OpenJDK 64-Bit Server VM (build 21+36-1738 or later, mixed mode)
"Command 'java' not found" Issue
If you receive an error message stating "Command 'java' not found," it is likely that the system cannot find the newly installed Java executable. In this case, you need to configure your system's PATH variable to include the location of the Java executable.
To do this, open the file /etc/environment in a text editor with root permissions:
sudo nano /etc/environment
Add the following line to the end of the file:
JAVA_HOME=/usr/lib/jvm/java-21-openjdk-amd64
Save and exit the file, then reload the environment changes:
source /etc/environment
Now you should be able to run the 'java' command.
Running a Java Program
To demonstrate the installation, create a simple Java program using a text editor:
nano HelloWorld.java
Enter the following code:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Save and exit the file, then compile the Java program using the javac compiler:
javac HelloWorld.java
This will create a file named HelloWorld.class in the current directory. Now you can run the program using the java executable:
java HelloWorld
You should see the output "Hello, World!" indicating that the program executed successfully.
- Debian 12.7.0 (64-bit, English) was used as the target distribution.
- The OpenJDK 21 package was installed using the apt package manager.
- The system's PATH variable was configured to include the location of the Java executable.
- A simple Java program was written, compiled, and executed using the installed Java tools.
References
- OpenJDK website: https://openjdk.org/
- Debian OpenJDK package: https://packages.debian.org/source/sid/openjdk-21