Read in 4 minutes

last updated 

How to install Java on Debian 9

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.

Prerequisites

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

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.

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

sudo apt install default-jdk

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_181"
OpenJDK Runtime Environment (build 1.8.0_181-8u181-b13-2~deb9u1-b13)
OpenJDK 64-Bit Server VM (build 25.181-b13, 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 Oracle Java 8

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 on your Debian machine:

  1. Download the Java archive using the following curl command:

    curl -L -b "oraclelicense=a" -O http://download.oracle.com/otn-pub/java/jdk/8u191-b12/2787e4a523244c269598db4e85c51e0c/jdk-8u191-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-8u191-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_191/bin/java" 1500
    sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/local/oracle-java-8/jdk1.8.0_191/bin/javac" 1500
    sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/local/oracle-java-8/jdk1.8.0_191/bin/javaws" 1500

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 typing:

java -version

The output should look something like this:

java version "1.8.0_191"
Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)

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/local/oracle-java-8/jdk1.8.0_191/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_191/bin/java   1500      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 as 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 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.