How to install Java on Debian 9

How to install Java on Debian 9
How to install Java on Debian 9

Install Java on Debian 9

Java is the popular programming language owned by Oracle. Java is used to build cross-platform applications. Over 3 billion devices run on Java. In this tutorial, we are going to install Java on Debian 9 using different methods.

Java has following three editions:

  1. Standard Edition(SE).
  2. Micro Edition(ME).
  3. Enterprize Edition(EE).

Following two implementations available for Java:

  1. Oracle Java.
  2. OpenJDK Java.

In this tutorial we are going to install Standard Edition (SE). There is no more difference in Oracle Java and OpenJDK Java as Oracle Java provides some additional enterprise features.

Prerequisites

Before you start to install Java on Debian 9. You must have the non-root user account on your system with sudo privileges.

Install Default OpenJDK

Currentally, OpenJDK 10 is the default version for Debian 9. To install OpenJDK 10 run following commands.

Update the package manager index typing following command.

sudo apt update

Enter the following command to install Default OpenJDK on Debian 9.

sudo apt install default-jdk

Confirm the installation and check the version typing following command.

java -version

Output should be:

openjdk version "10.0.1" 2018-05-29
OpenJDK Runtime Environment (build 10.0.1+10-Ubuntu-3ubuntu1)
OpenJDK 64-Bit Server VM (build 10.0.1+10-Ubuntu-3ubuntu1, mixed mode)

Install OpenJDK 8, 10, or 11

You can install the specific version of OpenJDK by using the following command. Replacing [VERSION_NUMBER] with the version number you want to install.

sudo apt install openjdk-[VERSION_NUMBER]-jdk

For example, if you want to install OpenJDK 8 which is the current LTS version of OpenJDK then just run following command.

sudo apt install openjdk-8-jdk

Install Java From Oracle Website

As web8team repository only contains Java 8. If you want to install Java 10 then download the package from Oracle Official Website. Here we are going to install Java 10 while you can choose another version.

Download Oracle Java 10 using the following command.

curl -L -b "oraclelicense=a" -O http://download.oracle.com/otn-pub/java/jdk/10.0.2+13/19aef61b38124481863b1413dce1855f/jdk-10.0.2_linux-x64_bin.tar.gz

Create a new directory for java installation.

sudo mkdir /usr/local/oracle-java-10

Now extract downloaded Java file inside the recently created directory.

sudo tar -zxf jdk-10.0.2_linux-x64_bin.tar.gz -C /usr/local/oracle-java-10

Now run following command to create alternatives. Which will create symbolic links for default commands.

sudo update-alternatives --install "/usr/bin/java" "java" "/usr/local/oracle-java-10/jdk-10.0.2/bin/java" 1500
sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/local/oracle-java-10/jdk-10.0.2/bin/javac" 1500
sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/local/oracle-java-10/jdk-10.0.2/bin/javaws" 1500

Setting up default Java Version

If you have installed multiple versions of Java on your system then you can change the default version running following command.

Check Current Java Version running following command.

java -version

Now to change default Java version run following command.

sudo update-alternatives --config java

The output should be:

There is 1 choice for the alternative java (providing /usr/bin/java).

  Selection    Path                                           Priority   Status
---------------------------------------------------------------------------------------
  0            /usr/lib/jvm/java-8-oracle/jre/bin/java          1081      auto mode
  1            /usr/lib/jvm/java-8-oracle/jre/bin/java          1081      manual mode
* 2            /usr/local/oracle-java-10/jdk-10.0.2/bin/java    1500      manual mode

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

NOTE: to change version just enter the number in the selection column

Set the JAVA_HOME Environment Variable

To set <code>JAVA_HOME</code> environment variable run following command and copy the path you want to be set as <code>JAVA_HOME</code>.

sudo update-alternatives --config java

Run the following command to set <code>JAVA_HOME</code> variable.

sudo nano /etc/environment

Add the following line to the end of file

JAVA_HOME="THE PATH YOU HAVE COPIED"

Apply the changes by running following command.

source /etc/environment

Now verify <code>JAVA_HOME</code> path typing:

echo $JAVA_HOME

Conclusion

You have successfully learned How to install Java on Debian 9. If you have any queries regarding this then please don’t forget to comment below.