How To Install Java with Apt on Debian 9 | DigitalOcean

Introduction

Java and the

JVM (the Java virtual machine) are required for many types of software, including Tomcat, Jetty, Glassfish, Cassandra, and Jenkins

.

In this guide, you will install various versions of the Java Runtime Environment (JRE) and Java Developer Kit (JDK) using apt. It will install OpenJDK as well as official Oracle packages. Next, you’ll select the version you want to use for your projects. When you’re done, you can use the JDK to develop software or use the Java Runtime to run software.

Prerequisites

To follow this tutorial, you will need:

A Debian 9 server

  • configured following the tutorial in the Debian 9 Server Initial Setup Guide, including a non-root user with sudo access and a firewall

.

Installing the default JRE/JDK

The easiest option to install Java is to use the version packaged with Debian. By default, Debian 9 includes Open JDK, which is an open source variant of the JRE and JDK.

This package will install OpenJDK version 1.8, which is compatible with Java 8. Java 8 is the current long-term support release and remains widely supported, although public maintenance ends in January 2019.

To install this version,

first update the package index

: sudo apt update

Next, check if

Java is already installed: java -version

If Java is not currently installed, you will see

the following output: Output-bash: java: command not found

Run the following command to install OpenJDK:

  1. sudo apt
  1. install default-jre

This command will install the Java Runtime Environment (JRE). This will allow you to run almost all Java software.

Verify the installation with

: java -version You

will see the following output:

Outputopenjdk version “1.8.0_181” OpenJDK Runtime Environment (build 1.8.0_181-8u181-b13-1~deb9u1-b13) OpenJDK 64-Bit Server VM (build 25.181-b13, mixed mode) You may need Java Development Kit (JDK)

in addition to the JRE to compile and run some specific Java-based software. To

install the JDK, run the following command, which will also install the JRE:

  1. sudo apt install default-jdk

Verify that the JDK

is installed by checking

the version of javac, the Java compiler:

  1. javac -version

You will see the following output

: Outputjavac 1.8.0_181

Next, let’s see how to install the official Oracle JDK and JRE. Installing

the Oracle JDK If you want to install the Oracle JDK

,

which is the official version distributed by Oracle

, you will need to add a new package repository for the version you want to use.

First, install the software-properties-common package that adds the apt-get-repository command that you will use to add additional repositories to your feed list.

Install software-properties-common with: sudo apt install

  1. software-properties-common

With this installed, you can

install Oracle Java. Installing Oracle Java 8

To install Java 8, which is the current long-term support release, first add your package

repository:

  1. sudo add-apt-repository ppa:webupd8team/java

When you add the repository, you will see a message like this:

Oracle Java Output (JDK) Installer (automatically downloads and installs Oracle JDK8). There are no actual Java files in this PPA. Important: > Why Oracle Java 7 and 6 installers no longer work: http://www.webupd8.org/2017/06/why-oracle-java-7-and-6-installers-no.html Update: Oracle Java 9 has reached End of Life: http://www.oracle.com/technetwork/java/javase/downloads/jdk9-downloads-3848520.html The PPA supports Ubuntu 18.04, 17.10, 16.04, 14.04 and 12.04. More information (and Ubuntu installation instructions): – for Oracle Java 8: http://www.webupd8.org/2012/09/install-oracle-java-8-in-ubuntu-via-ppa.html Debian installation instructions: – Oracle Java 8: http://www.webupd8.org/2014/03/how-to-install-oracle-java-8-in-debian.html For Oracle Java 10, see a different PPA: https://www.linuxuprising.com/2018/04/install-oracle-java-10-in-ubuntu-or.html More information: https://launchpad.net/~webupd8team/+archive/ubuntu/java Press [ENTER] to continue or ctrl-c to cancel the addition

Press ENTER to continue. It will try to import some GPG signing keys, but it will not be able to find any valid ones

: Outputgpg: keybox ‘/tmp/tmpgt9wdvth/pubring.gpg’ created gpg: /tmp/tmpgt9wdvth/trustdb.gpg: trustdb created gpg: key C2518248EEA14886: public key “Launchpad VLC” imported gpg: no trusted keys found ultimately gpg: Total number processed: 1 gpg: imported: 1 gpg: no valid OpenPGP data found.

Run the following command to manually add the GPG key for the repository source

:

  1. apt-key adv -keyserver keyserver.ubuntu.com -recv-keys C2518248EEA14886

Then update your

package list: sudo apt update

Once the package list is updated, install Java 8:

  1. sudo apt
  1. install oracle-java8-installer

Your system will download the Oracle JDK and ask you to accept the license agreement. Accept the agreement and the JDK will be installed.

Installing Oracle Java 10 To install Oracle Java 10

, first add your

repository:

  1. sudo add-apt-repository ppa:linuxuprising/java

You will see this message:

Oracle Java 10

Installer Exit Java binaries are not hosted in this PPA due to the license. The packages in this PPA download and install Oracle Java 10 (JDK 10), so a working Internet connection is required. The packages in this PPA are based on the WebUpd8 Oracle Java PPA packages: https://launchpad.net/~webupd8team/+archive/ubuntu/java Built for https://www.linuxuprising.com/ users Problems or suggestions? Leave a comment here: https://www.linuxuprising.com/2018/04/install-oracle-java-10-in-ubuntu-or.html More information: https://launchpad.net/~linuxuprising/+archive/ubuntu/java Press [ENTER] to continue or ctrl-c to cancel

the addition Press ENTER to continue with the installation. As with Java 8, you will see a message about invalid signing keys

: Outputgpg: keybox ‘/tmp/tmpvuqsh9ui/pubring.gpg’ created gpg: /tmp/tmpvuqsh9ui/trustdb.gpg: trustdb created gpg: key EA8CACC073C3DB2A: public key “Launchpad PPA for Linux Uprising” imported gpg: Total number processed: 1 gpg: imported: 1 gpg: No valid OpenPGP data found.

Run this command to import the required

key: sudo apt-key adv -keyserver keyserver.ubuntu.com -recv-keys EA8CACC073C3DB2A Then update your package list: sudo apt update

Once the package list is updated, install Java 10:

  1. sudo apt
  1. install oracle-java10-installer

Your system will download the Oracle JDK and ask you to accept the license agreement. Accept the agreement and the JDK will be installed.

Now let’s see how to select which version of Java you want to use.

Java

Management

You can have multiple installations of Java on one server. You can configure which version is the default for use on the command line by using the update-alternatives command.

  1. sudo update-alternatives -config

java

Here’s what the output would look like if you had installed all versions of Java in this tutorial:

There are 3 options for alternative java (providing /usr/bin/java). Priority status of selection path – 0 /usr/lib/jvm/java-10-oracle/bin/java 1091 automatic mode * 1 /usr/lib/jvm/java-10-oracle/bin/java 1091 manual mode 2 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 manual mode 3 /usr/lib/jvm/java-8-oracle/jre/bin/java 1081 manual mode Press <enter> to keep the current option[*], or type the selection number:

Choose the number associated with the Java version to use as the default, or press ENTER to leave the current settings in place.

You can do this for other Java commands,

such as the compiler (

javac):

  1. sudo update-alternatives -config javac

Other commands for which this command can be executed include, but are not limited to: keytool, javadoc, and jarsigner

.

Let’s set

the environment variable JAVA_HOME below. Setting the environment variable JAVA_HOME

Many programs written with Java use the JAVA_HOME environment variable to determine the Java installation location. To

set this environment variable, first determine where Java is installed.

Use the

update-alternatives command again:

  1. sudo update-alternatives -config

java

This command displays each Java installation along with its installation

path: Priority status of output selection path: 0 /usr/lib/jvm/java-10-oracle/bin/java 1091 automatic mode * 1 /usr/lib/jvm/java-10-oracle/bin/java 1091 manual mode 2 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 manual mode 3 /usr/lib/jvm/java-8-oracle/jre/bin/java 1081 manual

mode In this case, the installation paths are as follows:

Oracle Java 10 is located in /usr/lib/jvm/java-10-oracle/jre/bin/java. Oracle Java 8 is located in /usr/lib/jvm/java-8-oracle/jre/bin/java.

  • OpenJDK 8 is located in /usr/lib/jvm/java-8-openjdk-amd64/jre
  • /bin/java. These paths show the path to the java executable. Copy the path of your preferred installation, excluding the final

  • bin/java
  • component.

Then open

/etc/environment using nano or your favorite text editor:

  1. sudo nano /etc/environment

At the end of this file, add the

following line, making sure to replace the highlighted path with your own copied path:

JAVA_HOME=”/usr/lib/jvm/java-8-oracle/jre” Modifying this file

will set the JAVA_HOME path for all users on your system

.

Save the file and exit the editor.

Now reload this file to apply the changes to Your current session

: source /etc/environment

Verify that the environment variable is

set:

  1. echo $JAVA_HOME

You will see the path you just set:

Output/usr/lib/jvm/java-8-oracle/jre

Other users will need to run the source /etc/environment command or log off and log back in to apply these settings.

Conclusion

In this tutorial, you installed several versions of Java and learned how to manage them. You can now install software that runs in Java, such as Tomcat, Jetty, Glassfish, Cassandra, or Jenkins.