How To Install Java on Debian 11

Debian 11 is quite popular among developers and professionals using Java. So it is necessary to learn how to install Java on Debian 11 if you plan to make use of this language. Or you need to run a program that uses this technology that is still in use today.

Introducing to Java

Java is a class-based, object-oriented programming language. It was born with the goal that programmers would only have to write the code for a program once, and that the program would run on any device. To achieve this, Java has a JVM that does the translations and implementations.

When Java is combined with other powerful tools, it allows us to create complete, robust, scalable, portable, and distributed applications.

Java is a versatile programming system with a multitude of applications since its scope of application is so wide that Java is used in both mobile phones and household appliances.

So, let’s install it.

Install Java on Debian 11

Java is owned by Oracle but thanks to the free OpenJDK implementation we can install it from the official Debian 11 repositories. So first you have to open in terminal

After that, update Debian 11 as follows

sudo apt update
sudo apt upgrade

Then you can install Java version 11 like this

sudo apt install default-jdk

Although the command does not specify it, the default-jdk package refers to Java 11 which is the latest LTS version of this language.

Also, this package installs not only the JDK (libraries, compiler, and other tools to develop with the language) but also the JRE (JVM and other tools to run Java applications) so the installation is quite complete.

Although if all you need is the JRE and only run JAVA applications then you have to execute the following command

sudo apt install default-jre

However, if you are an enthusiast of the language and you like to try its new features, you can install version 17 which is the latest version available. This can also be found in the official Debian repository.

So, to install it, just run

sudo apt install openjdk-17-jdk

Or if you just want the JRE

sudo apt install openjdk-17-jre

So there is no problem if you have two or more versions of Java installed, but you need to configure your system.

Configuring the system before using Java on Debian 11

There is a command that allows us to define a Java version as the default version on the system. Of course, if you have only one version of Java installed, then there is not much of a trick here.

So, run

sudo update-alternatives – config java

If you have a single version of Java installed, then you will see an output screen like this

There is only one alternative in link group java (providing /usr/bin/java): /usr/lib/jvm/java-11-openjdk-amd64/bin/java
Nothing to configure.

But if like me you have installed several versions of Java then you will see the following screen

Java on Debian 11
Java on Debian 11

Where each installed Java version is identified with a number. And then we are asked which version we want to be the default. Just type in the number associated with the version.

To check the changes run

java – version
openjdk 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)

Now it’s time to set the environment variable. When you ran the command

sudo update-alternatives – config java

The PATH column refers to the path where it is installed. So we have to add this path as a Java variable for applications and configuration to be done properly.

In the case of Java 11 installed via OpenJDK, the path is /usr/lib/jvm/java-11-openjdk-amd64/bin/java.

So, open the file /etc/environment.

sudo nano /etc/environment

And add the following line

JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"

Save the changes and close the editor.

To apply the changes you need to run

source /etc/environment

So, Java is installed and ready for you to play with.

Conclusion

In this post, you have learned how to install Java on Debian 11 using the official repositories which is the most recommended and secure way to do it. We have also made it ready for you to start working with it.

So, what do you think about Java? Do you like it? have you used it?