How To Install Java on Debian 12
In this tutorial, we will show you how to install Java on Debian 12. For those of you who didn’t know, Java is a versatile programming language, that finds its application in numerous software solutions. The installation of Java involves two key components: the Java Development Kit (JDK) and the Java Runtime Environment (JRE). The JDK facilitates Java application development, while the JRE allows you to run Java applications. Selecting the appropriate JDK version is crucial. Consider your project requirements and compatibility factors when making this decision. As of the time of writing, the latest JDK version is Java 17.
This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo
‘ to the commands to get root privileges. I will show you the step-by-step installation of Java programming language on a Debian 12 (Bookworm).
Prerequisites
- A server running one of the following operating systems: Debian 12 (Bookworm).
- It’s recommended that you use a fresh OS install to prevent any potential issues.
- SSH access to the server (or just open Terminal if you’re on a desktop).
- An active internet connection. You’ll need an internet connection to download the necessary packages and dependencies for Java.
- A
non-root sudo user
or access to theroot user
. We recommend acting as anon-root sudo user
, however, as you can harm your system if you’re not careful when acting as the root.
Install Java on Debian 12 Bookworm
Step 1. Before we install any software, it’s important to make sure your system is up to date by running the following apt
commands in the terminal:
sudo apt update sudo apt install apt-transport-https lsb-release ca-certificates curl dirmngr gnupg
This command will refresh the repository, allowing you to install the latest versions of software packages.
Step 2. Installing Java on Debian 12.
To benefit from a well-maintained Java installation, it is recommended to add the official Debian repository. Execute the following commands in your terminal:
sudo apt install curl dirmngr gnupg
Now import the repository’s key, and run:
sudo sh -c 'echo "deb http://deb.debian.org/debian $(lsb_release -sc) main contrib non-free" >> /etc/apt/sources.list.d/debian.list' sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 8B48AD6246925553
OpenJDK, an open-source alternative to Oracle JDK, is recommended for its compatibility and security. Execute the following commands to install OpenJDK:
sudo apt update sudo apt install openjdk-17-jdk
Step 3. Configuring Environment Variables.
To ensure the proper execution of Java applications, it is essential to set up the necessary environment variables. Open the bash configuration file using the command:
nano ~/.bashrc
Add the following lines at the end of the file:
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64 export PATH=$JAVA_HOME/bin:$PATH
Save the changes and exit the text editor. To apply the modifications, run:
source ~/.bashrc
To confirm that Java is installed correctly, check the installed Java version by executing the command:
java -version
The output should display the Java version you installed, indicating a successful installation.
Step 4. Running a Sample Java Program.
To validate the functionality of your Java installation, let’s run a simple “Hello World” program. Now we create a new file named HelloWorld.java
using your preferred text editor and add the following content:
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } }
Save the file and exit the text editor. Next, compile the program using the following command:
javac HelloWorld.java
If no errors are reported, execute the compiled program using:
java HelloWorld
You should see the output “Hello, World!” displayed on the terminal, confirming that Java is functioning correctly.
Step 5. Troubleshooting and Common Issues:
During the installation process, you may encounter some common issues. Here are a few troubleshooting tips:
-
If you receive an error message indicating an incompatible version of Java, ensure that you have selected the appropriate JDK version for your project.
-
If the installation fails or you experience package-related issues, try updating the package repositories by executing the following commands:
sudo apt clean sudo apt update
Congratulations! You have successfully installed Java. Thanks for using this tutorial for installing the latest version of Java programming language on Debian 11 Bookworm. For additional help or useful information, we recommend you check the official Java website.