How to install Java SE Development Kit on Debian Linux

Objective

The objective of this guide is to provide a simple to follow steps on how to install Java SE Development Kit on Debian Linux.

OS and Software Version

  • Operating System: Debian 9 (Stretch)
  • Software: Java SE Development Kit 8

Requirements

Privileged access to your Debian Linux is required. Optionally, curl will be used to download an appropriate Java JDK tarball.

Difficulty

EASY

Conventions

  • # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
  • $ – requires given linux commands to be executed as a regular non-privileged user

Instructions

Download

Let’s start by downloading Java JDK tarball from the official Oracle website. Navigate to Oracle Java JDK page and locate an appropriate system architecture relevant tarball. The website requires the Oracle Binary Code License Agreement for Java SE license to be accepted prior to download. From this reason, there are two options on how to download Oracle Java JDK tarball.

Web Browser

The first option is to download desired Java JDK tarball directly using the web browser, while also accepting the required Oracle Binary Code License Agreement for Java SE license by clicking the provided radio button. Once done, copy the downloaded file using SCP or FTP client into a final destination.

Curl

In case that you are attempting to install Java JDK remotely and have absolutely no access to Graphical User Interface and web browser use the following curl method to download Java JDK using a command line.

First, obtain a correct download URL by using the curl command:

$ curl -s http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html | grep "otn-pub" | cut -d \" -f12

The above command outputs a bunch of URLs for your selection. To initiate the download of the desired Java file, copy its URL and start the download using the curl command while accepting the requested Jave license. For example:

$ curl -LOb "oraclelicense=a" http://download.oracle.com/otn-pub/java/jdk/8u121-b13/e9e7ea248e2c4826b92b3f075a80e441/jdk-8u121-linux-x64.tar.gz

Install Java JDK

At this stage, we should have an appropriate Java JDK tarball within our current working directory:

$ ls
jdk-8u121-linux-x64.tar.gz

Create a target Java JDK installation directory:

# mkdir /opt/java-jdk

Extract the previously downloaded Java JDK tarball:

# tar -C /opt/java-jdk -zxf jdk-8u121-linux-x64.tar.gz

Set Oracle Java as default

Currently, the system does not recognise our Java JDK installation:

$ update-alternatives --list java
update-alternatives: error: no alternatives for java
$ java
bash: java: command not found

Use the update-alternatives command to inlcude both, java and javac as part of the system’s Java environment.Please replace the below path to java binaries where appropriate to reflect your downloaded java version:

# update-alternatives --install /usr/bin/java java /opt/java-jdk/jdk1.8.0_121/bin/java 1   
update-alternatives: using /opt/java-jdk/jdk1.8.0_121/bin/java to provide /usr/bin/java (java) in auto mode
# update-alternatives --install /usr/bin/javac javac /opt/java-jdk/jdk1.8.0_121/bin/javac 1
update-alternatives: using /opt/java-jdk/jdk1.8.0_121/bin/javac to provide /usr/bin/javac (javac) in auto mode

Confirm Java JDK Setup

Java JDK should now be installed and ready for use:

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