In this tutorial, we will walk through installing Java on Debian 9.  Java is one of the most popular programming languages used to build  different kinds of applications and systems. Applications developed in  Java are scalable, flexible, and maintainable.

There are two  different Java packages, Java Runtime Environment (JRE) and Java  Development Kit (JDK). If you only want to run Java programs, then you  need JRE, and if you are Java developer, then you will need JDK, which  includes JRE and development/debugging tools and libraries.

There  are also two different implementations of Java, OpenJDK and Oracle  Java, with almost no differences between them except that Oracle Java  has a few additional commercial features.

If  you are not sure which Java implementation and version to use, the  general recommendation is to stick to the default OpenJDK version  available on Debian 9.

Prerequisites

Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges.

Install OpenJDK 8

OpenJDK  8, the open-source implementation of the Java Platform, is the default  Java development and runtime in Debian 9. The installation is simple and  straightforward.

Use the following command to install OpenJDK 8 JDK from the standard Debian repositories:

sudo apt update

Once the installation is complete, you can verify it by checking the Java version:

java -version

The output should look something like this:

openjdk version "1.8.0_212"
OpenJDK Runtime Environment (build 1.8.0_212-8u212-b01-1~deb9u1-b01)
OpenJDK 64-Bit Server VM (build 25.212-b01, mixed mode)

That’s it! At this point, you should have successfully installed Java on your Debian system.

JRE is included in the JDK package. If you need only JRE, install the default-jre package:

Install OpenJDK 11

At  the time of writing, the latest LTS version of Java is version 11. This  version is available from the Debian Backports repository.

First, add Backports to your system’s software repository list:

echo 'deb http://ftp.debian.org/debian stretch-backports main' | sudo tee /etc/apt/sources.list.d/stretch-backports.list

Once the repository is enabled, update apt sources and install Java 11 using the following commands:

sudo apt update

Installing Oracle Java

Before installing Oracle Java, make sure you read the Oracle JDK License. The license permits only non-commercial use of the software, such as personal use and development use.

Oracle Java 11 can be installed from the Linux Uprising PPA.

The following steps describe how to install Oracle Java 11 on Debian 9:

Start by installing the necessary packages:

sudo apt install dirmngr gnupg

Import the PPA public key and enable the repository with the following command:

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 73C3DB2A

Once the repository is added, update the packages list and install the oracle-java11-installer package by typing:

sudo apt update

You will be prompted to accept the Oracle license.

Verify the installation by running the following command which will print the R version:

java -version
java version "11.0.2" 2019-01-15 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.2+9-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.2+9-LTS, mixed mode)

Set the default version

If  you have multiple Java versions installed on your Debian machine to  check what version is set as the default Java version type:

java -version

The output should look something like this:

openjdk version "11.0.3" 2019-04-16
OpenJDK Runtime Environment (build 11.0.3+1-Debian-1bpo91)
OpenJDK 64-Bit Server VM (build 11.0.3+1-Debian-1bpo91, mixed mode, sharing)

To change the default version, use the update-alternatives system command:

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-11-openjdk-amd64/bin/java      1111      auto mode
  1            /usr/lib/jvm/java-11-openjdk-amd64/bin/java      1111      manual mode
  2            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081      manual mode

Press <enter> to keep the current choice[*], or type selection number: 

You will be presented with a list of all installed Java  versions on your Debian system. Enter the number of the version you want  to be used as a default and press Enter.

Uninstall Java

If for any reason you want to uninstall the Java package, you can uninstall it like any other package installed with apt.

For example, if you want to uninstall the default-jdk package simply run:

sudo apt remove default-jdk

Conclusion

Now  that you have learned how to install and manage different Java versions  on your Debian server, your next step could be to install one of the  many applications which run on Java, such as Tomcat, JBoss/WildFly, Apache Maven, Glassfish, Elasticsearch, Cassandra, Jenkins, etc.

If you have any questions, feel free to leave a comment.