Java is a high-level, class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible. Java was developed by Sun Microsystems (which is now the subsidiary of Oracle) in the year 1995. James Gosling is known as the father of Java.
Java and the JVM (Java’s virtual machine) are required for many kinds of software, including Tomcat, Jetty, Glassfish, Cassandra and Jenkins.
In this guide we are going to explore how to install Java Runtime Environment (JRE) and the Java Developer Kit (JDK) in Debian 11.
Also Check:
- How to install Java 17 in Ubuntu 20.04
- How to install Java 17 in Fedora 35
- How to install Java 17 On Rocky Linux 8/Centos 8
- Installing and setting up Jenkins on Ubuntu 20.04
Prerequisites
- Up to date Debian 11 system
- Root access or user with sudo access
- Internet access from the server
Table of content
- Ensuring that the server is up to date
- Installing java
- Testing the installation
- Toggling multiple Java versions
- Configuring the Environmental Variable
Ensuring that the server is up to date
Before proceeding, let us make sure that the server packages and repositories are up to date. Use these commads to achieve the goal:
sudo apt update
sudo apt -y upgrade
If there are packages to upgrade, the above comand may take a couple of minutes.
Let us also install some common packages that we might need. I use vim text editor, please make sure its installed using this command:
sudo apt install -y vim
Installing Java
Check whether java is installed in your sytem.
$ java -version
-bash: java: command not found
If you see
java: command not found
then it means that java is not installed. If java is installed, that command will print out the java version
Debian 11’s default repository included Java 17. This is the easiest way to install the JDK using the apt package manage.
You can search java using this command:
sudo apt-cache search openjdk | grep 17
Run the following command to install Java 17 on Debian 11. We will install both JDK and JRE in order to run Java applications and applets.
sudo apt install openjdk-17-jdk openjdk-17-jre -y
Once the installation is completed, run the following command in order to check if it works properly. If everything works fine, you should get an output similar to the one shown below.
$ java -version openjdk version "17-ea" 2021-09-14 OpenJDK Runtime Environment (build 17-ea+19-Debian-1) OpenJDK 64-Bit Server VM (build 17-ea+19-Debian-1, mixed mode, sharing)
To verify if the JDK is installed properly, we will check the version of javac, which is the Java compiler.
$ javac -version javac 17-ea
Testing the installation
Let us test that Java 17 is installed properly in this step by creating a simple script that prints out Java is installed properly
.
Create the directory and switch to it
mkdir simple-app
cd simple-app
I use vim to create and edit text files in my system. Use the text editor of your choice in this step. I will create a file Simple.java
using this command:
sudo vim Simple.java
Add these content to the file
public class Simple{
public static void main(String[] args) {
System.out.println("Citizix - <span style="font-size: calc(11px + 0.2em);">Java is installed properly</span>");
}
}
Now that we have created our program, we need to compile the Java source code into bytecode (class file) using the javac compiler.
javac Simple.java
You will get a new file called Simple.class
, which is the compiled Java class file.
$ ls
Simple.class Simple.java
The command above will only compile the Java source code into bytecode. In order to run the program, we run the java command with the name of our class file as an argument.
java Simple
If everything works well, you’ll see a message “
Citizix - Java is installed properly
” on the screen.
$ java Simple
Citizix - Java is installed properly
Toggling multiple Java versions
If you have multiple versions of Java installed in your system, you can configure which version the java
command defaults to.
Use this command to configure that
sudo update-alternatives --config java
You will be given a list of available java installations. In this case I have both java 11 and java 17 installed. The default version is marked with an asterisk(*). You can opt to use a different version by entering the number in the prompt.
$ sudo update-alternatives --config java
There are 2 choices for the alternative java (providing /usr/bin/java).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/lib/jvm/java-17-openjdk-amd64/bin/java 1711 auto mode
1 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 manual mode
2 /usr/lib/jvm/java-17-openjdk-amd64/bin/java 1711 manual mode
Press <enter> to keep the current choice[*], or type selection number: 1
update-alternatives: using /usr/lib/jvm/java-11-openjdk-amd64/bin/java to provide /usr/bin/java (java) in manual mode
In the above, I entered 1 to the promp to choose Java 11. Let us confirm the java version now:
$ java -version
openjdk version "11.0.12" 2021-07-20
OpenJDK Runtime Environment (build 11.0.12+7-post-Debian-2)
OpenJDK 64-Bit Server VM (build 11.0.12+7-post-Debian-2, mixed mode, sharing)
Configuring the Environmental Variable
Some Java applications require the JAVA_HOME environmental variable in order to run properly. Some programs are very specific in how they are executed. If JAVA_HOME isn’t set, you’ll get an error. Setting JAVA_HOME will prevent this problem from arising.
To check which Java installations and paths exist, use this command:
sudo update-alternatives --config java
In my case, I get the Java installation binary path to be this /usr/lib/jvm/java-17-openjdk-amd64/bin/java
We are going to set this path in an enviroment file
/etc/environment
where all the apps can access.
Open the file with vim:
sudo vim /etc/environment
Now we need to add a line exporting the Java home to the file like this
export JAVA_HOME=...
making the value to be the path we got for our Java installation:
<meta charset="utf-8"><code>export JAVA_HOME=<span style="font-size: calc(11px + 0.2em);">/usr/lib/jvm/java-17-openjdk-amd64/bin/java</span>
</code>
This file will set the JAVA_HOME path of OpenJDK 17 as environmental variables for all users in the system.
To apply the change, run the following command.
source /etc/environment
To verify that the path variable has been applied, run the following command.
echo $JAVA_HOME
The path should be set to the path of OpenJDK 17, which you have copied above. This is the value in my case:
$ echo $JAVA_HOME
/usr/lib/jvm/java-17-openjdk-amd64/bin/java
From now on, you don’t have to set the JAVA_HOME path every time when using Java programs. Also, if you want to use other OpenJDK versions instead of 17, adapt the path accordingly.
Conclusion
In this tutorial, you have learned how to install Java 17 on a Debian 11 system. If you need further information about the Java language, please refer to its official documentation.