Java is one of the most popular programming language. It is owned by Oracle and used to build different kinds of applications. We will install various versions of the Java Runtime Environment (JRE) and the Java Developer Kit (JDK). Also, we will install OpenJDK from official packages from Oracle. In this tutorial, you will learn how to install Java on Debian 9 system.
You should logged in on Debian 9 server using non-root user account with sudo privileges.
Install Default OpenJDK
Java installation process is easy and straight forward. OpenJDK 8 JDK is available to install from the standard Debian repositories. Run the below command to install:
First update the package manager index using below command:
sudo apt update
Now install Java by typing following command:
sudo apt install default-jdk
Once the installation is finished you can confirm installation by checking version of jdk by:
It will show below output:
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)
All done! Java is installed on you Debian system.
Install Specific Version of OpenJDK
Currently, latest stable version is OpenJDK 11. This version is available from the Debian Backports repository.
So first we need to add Backports to your system software repository:
echo 'deb http://ftp.debian.org/debian stretch-backports main' | sudo tee /etc/apt/sources.list.d/stretch-backports.list
Once it is enabled you need to update package manager and then go ahead for install.
sudo apt update
You can install OpenJDK with your specified version. You just have to add version as per below syntax.
sudo apt install openjdk-[VERSION]-jdk
For example, latest stable version is OpenJDK 11. So we can install it using below command:
sudo apt install openjdk-11-jdk
Installing Java From Oracle
Oracle provides license permits only non-commercial use, for personal use and development use only. So before going to install it read the Oracle JDK License. Here we are going to install Java 11.
Follow the below steps to install Java from Oracle:
At first, we install the necessary packages using below command:
sudo apt install dirmngr gnupg
Now you need to enable repositories after importing the PPA public key by following commands:
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 73C3DB2A
echo ‘deb http://ppa.launchpad.net/linuxuprising/java/ubuntu bionic main’ | sudo tee /etc/apt/sources.list.d/linuxuprising-java.list
Once the repository is added and enabled, update the packages list index:
sudo apt update
Next, run the below command to install package:
sudo apt install oracle-java11-installer
You will be prompted to accept and agree for the Oracle license. Accept it to continue installation.
Check the installed version by running the following command:
It will show you output as below:
java version "11.0.2" 2019-01-15 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.2+9-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.2+9-LTS, mixed mode)
Setting up default Java Version
If your Debian system have multiple version of Java then you can set default version as per your choice.
First, check the current default version by typing:
To change the default version use the
update-alternatives system command as below:
sudo update-alternatives --config java
There are 2 choices for the alternative java (providing /usr/bin/java).
Selection Path Priority Status
0 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 auto mode
1 /usr/lib/jvm/java-11-openjdk-amd64/bin/java 1111 manual mode
2 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 manual mode
Press to keep the current choice[*], or type selection number:
You can see list of all installed Java versions. Select appropriate selection number to set as default version.
You can uninstall the Java package same as other packages. For example, if you want to uninstall the
default-jdk package simply run:
sudo apt remove default-jdk
You have learned How to install and manage multiple Java versions on Debian 9 system. At the end you also learned how to set default Java version.
If you have any queries regarding this tutorial feel free to comment below.