Java and the Java Virtual Machine are required for many kinds of software. In this article, you will learn how to install Java with Apt on Debian 10. Follow this guide to review the installation of Java in different versions.
You would install OpenJDK 11 as well as the official Java 11 software from Oracle and select your prefers version to use for your projects. Then, you can use the JDK to develop software or use the Java Runtime to run the software.
Prerequisites
The tutorial may be more useful if you know:
- a non-root user with sudo privileges
- To set up, follow our Initial Setup with Debian 10
Table of Contents
How to install Java with Apt on Debian 10
Let’s walk through the following steps to finish this interesting and simple guide.
Installing the Default JRE/JDK
You will see the easiest option for installing Java, which is to use the version packaged with Debian. As the Debian 10 includes OpenJDK version 11 by default and is an open-source variant of the JRE and JDK, and is compatible with Java 11.
First, update your apt package index:
sudo apt update
To check if Java is already installed or not:
java -version
If not, you will see the following output
-bash: java: command not found
Use the following command to install OpenJDK:
sudo apt install default-jre
In this way, you will install the Java Runtime Environment (JRE), allowing you to run almost all Java software.
To verify the installation:
openjdk version "11.0.4" 2019-07-16 OpenJDK Runtime Environment (build 11.0.4+11-post-Debian-1deb10u1) OpenJDK 64-Bit Server VM (build 11.0.4+11-post-Debian-1deb10u1, mixed mode, sharing)
To compile and run some specific Java-based software, you may also need the Java Development Kit (JDK).
sudo apt install default-jdk
Check the version of javac to see if the JDK is installed or not.
javac -version
javac 11.0.4
Buy Linux Virtual Private Server
Installing the Oracle JDK
You must download some files directly from Oracle to install the official Oracle JDK.
First, download Java from Oracle’s website.
Downloading Oracle’s Java SE Software Package
Since there have been some changes in the way Oracle handles Java licensing, create an Oracle account, and download the software directly from their website before continuing with the installation.
Note: Use the Oracle’s account creation page to create an account, then navigate to Oracle’s Java SE Downloads page.
Important: Make sure to download the correct version of Java. As it is necessary to match what the installer is expecting. Visit the package listing of the installer’s software repository to check it.
Find the oracle-java11-installer-local… files:
As you see, t the installer is expecting version 11.0.4. Ignore any number that comes after the – hyphen in the package version number. Since you know the correct version number, scroll down the Java download page until you find the correct version.
To see the below screen, press the download button.
Then, select the Accept License Agreement radio button, then click the .tar.gz package for Linux, and your download will begin. If you are asked, log in to your Oracle account again.
Next, get the downloaded file onto our server by any file transfer software you’re comfortable with.
Use scp to upload the file to your server. To adjust the file paths as needed for your systems:
scp ~/Downloads/jdk-11.0.4_linux-x64_bin.tar.gz noodi@your_server_ip:~
After getting the correct Java software package up on your server, add the repo that contains the installer we’ll use to install Java.
Adding the Installer’s Apt Repository
In this step, you must install the software-properties-common package, which adds the add-apt-repository command to your system:
sudo apt install software-properties-common
And import the signing key used to verify the software you are about to install:
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys EA8CACC073C3DB2A
To add the repo to our list of package sources:
sudo add-apt-repository ppa:linuxuprising/java
Oracle Java 11 (LTS) and 12 installer for Ubuntu, Linux Mint and Debian. Java binaries are not hosted in this PPA due to licensing. The packages in this PPA download and install Oracle Java 11, so a working Internet connection is required. The packages in this PPA are based on the WebUpd8 Oracle Java PPA packages: https://launchpad.net/~webupd8team/+archive/ubuntu/java Created for users of https://www.linuxuprising.com/ Installation instructions (with some tips), feedback, suggestions, bug reports etc.: . . . Press [ENTER] to continue or ctrl-c to cancel adding it
To continue the installation, press ENTER. The probable message of no valid OpenPGP data found can be ignored.
Finally, update your package list to make the new software available for installation:
sudo apt update
Installing the Downloaded Java Software
At this point, create a specific directory that the installer uses to find the Java software package, then copy the .tar.gz file in:
sudo mkdir -p /var/cache/oracle-jdk11-installer-local/ sudo cp ~/jdk-11.0.4_linux-x64_bin.tar.gz /var/cache/oracle-jdk11-installer-local/
Then, you can install Oracle’s Java 11 by installing the oracle-java11-installer-local package, when the file copy is complete.
sudo apt install oracle-java11-installer-local
When the installer will ask you, accept the Oracle license agreement to let it extract the Java package and install it.
Anyway, you have multiple versions of Java installed, so let’s look at how to select which version of Java you want to use.
Managing Java
You can have multiple Java installations on one server. To configure which version is the default for use:
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-11-oracle/bin/java 1091 manual mode Press <enter> to keep the current choice[*], or type selection number:
If you want to use it as the default, choose the number associated with the Java version. Or to leave the current settings in place, press ENTER. (You can do this for other Java commands, such as the compiler javac
sudo update-alternatives --config javac
Setting the JAVA_HOME Environment Variable
To determine which Java installation location to use, many programs written in Java using the JAVA_HOME environment variable.
Determine where Java is installed, to set this environment variable.
sudo update-alternatives --config 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-11-oracle/bin/java 1091 manual mode
Then, you can see the installation paths below(path to the java executable.)
Oracle Java 11 is located at /usr/lib/jvm/java-11-oracle/bin/java.
OpenJDK 11 is located at /usr/lib/jvm/java-11-openjdk-amd64/bin/java.
Choose your favorite installation path and copy it ( excluding the trailing bin/java component). Open /etc/environment using your preferred text editor.
sudo nano /etc/environment
Add the following line, at the end of the file (the file may be blank initially).
JAVA_HOME="/usr/lib/jvm/java-11-oracle/"
Note: Make sure to replace the highlighted path with your own copied path.
When you modify this file, you will set the JAVA_HOME path for all users on your system.
You can save the file and exit the editor now and reload this file to apply the changes to your current session:
source /etc/environment
Next, verify that the environment variable is set:
echo $JAVA_HOME
/usr/lib/jvm/java-11-oracle/
Other users will need to execute the command source /etc/environment or log out and log back in to apply this setting.
Dear user, we wish this tutorial how to install Java with Apt on Debian 10 would be helpful for you, to ask any question or review the conversation of our users about this article, please visit Ask page. Also to improve your knowledge, there are so many useful tutorials ready for Eldernode training.