How to Install Java with Apt on Debian 10 

Java is an essential requirement for many types of Java-based applications, including Jenkins and Tomcat. Java Virtual Machine and Java Runtime Environment are part of the Java Development Kit. It is essential to learn how to install Java with Apt on Debian machines.

There are two implementations of Java, Oracle Java, and OpenJDK. There is no difference between the two except that Oracle Java consists of commercial features and requires a license for commercial use. In this tutorial, you will learn how to install using OpenJDK.

In this guide, you will learn how to install Java with Apt package manager on Debian 10. You will also learn how to install Java Virtual Machine (JVM) and Java Runtime Environment (JRE) as part of the Java Development Kit (JDK). 

1. Install the Default JRE with Apt on Debian 10

The default Debian 10 package manager consists of two flavors of JRE and JDK. Also, JRE includes all the required classes and binaries that you will need to run Java programs. You will use the default OpenJDK in this tutorial. By default, version 11 of OpenJDK exists in the Debian package manager. Let’s get started with the installation.

Firstly, update the existing packages using the apt update command. 

sudo apt update

Secondly, check if Java is already installed in your system. 

java -version

If it is not installed, you will get a similar output:

javac

Next, install the default JRE using the apt install command.

sudo apt install default-jre

This command will install JRE, through which you can run almost all Java applications. 

Lastly, verify the installation. 

java -version

You will get a similar output.

java version

2. Install JDK with Apt on Debian 10

For Java applications, you need JDK along with JRE to run some specific Java-based software and applications. To install JDK, use the apt install command. For example:

sudo apt install default-jdk

To verify the installation, check the javac version. For example:

javac -version

You will get a similar output:

OpenJDK

3. Manage Different Java Versions

For instance, you might have various Java versions installed on your Debian 10 systems. In this case, it is essential to learn how to manage Java versions and use the required version for your Java application. In addition, you can also configure a default version. 

There are two ways to achieve this: using java or javac compiler. For this step, run the update-alternatives command. For example:

sudo update-alternatives --config java
install java with apt on debian

Through this command, you can configure Java versions. The successful execution of the command will display a list of installed Java versions and ask you to choose the version by typing a number. The current choice will be represented by the [*] sign. 

Choose the version you want as a default, and hit “Enter key” to save the settings.

Alternatively, you can use the javac flag instead of java with the update-alternatives command. 

sudo update-alternatives --config javac

It will give you a similar list of choices, and you can select the suitable one. 

In addition, there are other commands which you can use for setting the default Java version. It includes jarsigner, keytool, and javadoc.

4. Set JAVA_HOME Variable in Debian 10

The next step is to add JAVA_HOME to the environment variable. Setting the environment variable will enable your operating system to find the required Java utilities and programs. Also, programs written in Java utilize this variable to determine which locally installed version of Java to use. 

To set the environment variable, first determine whether Java is installed or not. For this step, you can use the -v or –version flag. 

Secondly, use the update-alternatives command again. 

sudo update-alternatives --config java

This command will display the installed Java versions with their paths. In our case, it is located at /usr/lib/jvm/java-11-openjdk-amd64/bin/java. This path defines the executable Java file. Copy the complete path, excluding the /bin/java part. Then open the environment file located at /etc/environment using the nano or vim command. You can also use any other favorite text editor. 

sudo nano /etc/environment

The file might be blank in the beginning. Add the following lines in your file:

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

Make sure to use the path listed in your Debian 10 machine. Save the changes by pressing the “Ctrl + X key”. Then hit “Enter” to exit from the file. Editing this file would have modified the JAVA_HOME path for all users on your machine.

Next, reload the file to confirm the changes using the source command. For example:

source /etc/environment

Lastly, verify the JAVA_HOME environment variable using the echo command. For example: 

echo $JAVA_HOME

You will see the path that you saved on the screen. 

install java with apt on debian

Although the JAVA_HOME is set for all the users, other users still need to reload the file using the source command, log out and log back in to implement the settings. For example:

source /etc/environment

5. Uninstall Java

You can easily uninstall Java from Debian 10 machines using the apt remove command. For example, to remove default-jdk, execute the following command: 

sudo apt remove default-jdk

Successful execution of this command will remove the default OpenJDK. You can verify this using the java or javac command followed by the -v or –version flag. For example:

java -v
javac –version

In this article, you have learned how to install Java with Apt on Debian 10 machines. You also learned how to manage various Java versions and configure Java environment variables. You can now install Java-based applications such as Apache Tomcat and Jenkins. Check out our guide on Tomcat on Debian 10

If this guide helped you, please share it.

Leave a Reply
Related Posts