Debian - How to install Open JDK and Oracle JDK

By xngo on June 11, 2019

Install the Default JDK

The easiest way to install JDK is to install from the Debian repository.

# First update the package index
sudo apt-get update
 
# Install Open JDK.
sudo apt-get -y install default-jdk
 
# Check if Open JDK is installed.
java -version
    # openjdk version "1.8.0_212"
    # OpenJDK Runtime Environment (build 1.8.0_212-8u212-b01-1~deb9u1-b01)
    # OpenJDK 64-Bit Server VM (build 25.212-b01, mixed mode)

Install Oracle JDK

The Oracle JDK is not available in the default repository as it is not completely free. In a nutshell, the Oracle license says that it will not cost you for personal and development use. However, you have to pay to use it in production. Here is the complete license agreement.

# 1 - Download the DEB file from https://www.oracle.com/technetwork/java/javase/downloads/index.html
 
# 2 - Install DEB file.
sudo dpkg -i jdk-12.0.1_linux-x64_bin.deb
 
# 3 - Tell Debian that a new alternative java is installed.
#   Note: Path may differ depending on the java version that you installed.
sudo update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/jdk-12.0.1/bin/java" 0
sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/lib/jvm/jdk-12.0.1/bin/javac" 0
sudo update-alternatives --install "/usr/bin/jar" "jar" "/usr/lib/jvm/jdk-12.0.1/bin/jar" 0
 
# 4 - Select which java version to use: Type in the number having the path ".../jdk-12.0.1/...".
sudo update-alternatives --config java
 
# 5 - Check if Oracle JDK is being used as default.
java -version
    #java version "12.0.1" 2019-04-16
    #Java(TM) SE Runtime Environment (build 12.0.1+12)
    #Java HotSpot(TM) 64-Bit Server VM (build 12.0.1+12, mixed mode, sharing)

Set environment variable JAVA_HOME

Always set the environment path for JAVA_HOME so that other applications can find the location of the Java installation.

Put the following lines in /etc/profile.d/java.sh file for all users or in ~/.bash_profile file for single user. If the file doesn't, then create it.

export JAVA_HOME=/usr/lib/jvm/jdk-12.0.1
export PATH=$PATH:$JAVA_HOME/bin

Video showing step-by-step how to install JDK

Video showing how to install Oracle JDK 12

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.