Read in 3 minutes

last updated 

How to install Java on Debian 9

This tutorial is also available for:

centos 7 ubuntu 18.04

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 the 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 with the default OpenJDK version available on Debian 9.

Install OpenJDK

OpenJDK, the open source implementation of the Java Platform is the default Java development and runtime in Debian 9.

The installation is simple and straightforward.

Install OpenJDK 8 JDK

sudo apt install default-jdk

Install OpenJDK 8 JRE

sudo apt install default-jre
Advertisement

Install Oracle Java

In this section we will show you how to download and install the latest Oracle Java version 8 from the Oracle Website. Before downloading the Java tar.gz file you should check the Oracle’s downloads page if there is a new version available.

Follow the steps below to install the latest Oracle Java 8 from the Oracle Website:

  1. First, download the Java archive:

    curl -L -b "oraclelicense=a" -O http://download.oracle.com/otn-pub/java/jdk/8u181-b13/96a7b8442fe848ef90c96a2fad6ed6d1/jdk-8u181-linux-x64.tar.gz

    The command above will automatically accept the Oracle licence and download the Java tarball.

  2. Next, create a directory for the Java installation

    sudo mkdir /usr/local/oracle-java-8
  3. Extract the Java .tar.gz file to the previously created directory:

    sudo tar -zxf jdk-8u181-linux-x64.tar.gz -C /usr/local/oracle-java-8
  4. Once the files are extracted run the following commands to create new alternatives:

    sudo update-alternatives --install "/usr/bin/java" "java" "/usr/local/oracle-java-8/jdk1.8.0_181/bin/java" 1500
    sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/local/oracle-java-8/jdk1.8.0_181/bin/javac" 1500
    sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/local/oracle-java-8/jdk1.8.0_181/bin/javaws" 1500

Set the default version

We can check the version of our default Java, with:

java -version
java version "1.8.0_181"
Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)

If we have multiple Java versions installed on the server we can change the default version using the update-alternatives system:

sudo update-alternatives --config java
There are 2 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                            Priority   Status
------------------------------------------------------------
* 0            /usr/local/oracle-java-8/jdk1.8.0_181/bin/java   1500      auto mode
  1            /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java   1081      manual mode
  2            /usr/local/oracle-java-8/jdk1.8.0_181/bin/java   1500      manual mode

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

Enter the number when prompted and press Enter.

Uninstall Java

If for any reason you want to uninstall the Java package, you can uninstall it as any other package installed with apt. For example if you want to uninstall the default-jre package simply run:

sudo apt remove default-jre

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 runs on Java, such as Tomcat,, JBoss/WildFly, Jetty, Glassfish, WebLogic, Cassandra, Jenkins ..etc

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