This tutorial helps you to install Java on Debian 9 (Stretch) via Apt-Get. Java is a popular object-oriented, platform-independent programming language.
You can install Java based on your requirements. As an example, to run a java application you only need to install JRE (Java Runtime Environment) but for the development system, you need the JDK (Java Development Kit).
You can either install OpenJDK or Oracle Java on your system. This tutorial covers both options for you.
OpenJDK is an open source implementation of the Java Platform standard edition. You can install it directly from the default repository on Debian 9 system.
sudo apt install default-jre
sudo apt install default-jdk
Installing Oracle JAVA
You need to configure Apt repository on your system first before beginning Java installation on Debian 9. Execute following commands to import signing key and add Java PPA on Debian.
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys EEA14886 sudo add-apt-repository "deb http://ppa.launchpad.net/webupd8team/java/ubuntu trusty main"
Then install Java on your system. The package oracle-java8-installer is not actual Java package, It contains scripts which automatically configure Java on Debian 9 system.
sudo apt update sudo apt-get install oracle-java8-installer
You can also install Java 10 on Debian 9 using these instructions.
Switch Between Multiple Java Versions
At this step, you have installed multiple java versions on your system. But you need to switch to different Java version. Let’s execute below command from terminal and choose appropriate installed Java on Debian 9.
sudo update-alternatives --config java
See the above screenshot, I choose 2 which will configure Oracle Java 8 as default version on my Debian system.