Introduction:
Install Oracle Java JDK on Debian 11 are needed for a variety of software applications, including Cassandra, Glassfish, Tomcat, Jetty, and Jenkins.
This guide will show you how to Install Oracle Java JDK on Debian 11. We will install Oracle’s regular JDK along with OpenJDK. After that, you can decide which version to use for your projects. Once finished, you can use the Java Runtime to run applications or the Java Development Kit (JDK) to build programs.
Requirements:
To finish this tutorial, you will require:
One Debian 11 server with a non-root user who has sudo enabled. To get this installed, use our first Debian 11 server setup tutorial.
Step 1 — Installing Java:
The JDK, which provides software tools for developers like a compiler and debugger, the JRE, which executes Java programs, and two primary Java installation options—OpenJDK, an open-source Java implementation included with Debian, and Oracle JDK, the original Java version that is fully maintained by Oracle, the Java developers—are the two main components of installing Java.
Oracle has acknowledged both of these versions formally. Both are produced by Oracle, however OpenJDK gains from community contributions as well because it is an open-source project. However as of Java 11, the two systems are now functionally identical, according to Oracle. whatever license to install will depend on whatever you choose as the one that best suits your needs. Moreover, OracleJDK comes with the JRE installed, but OpenJDK lets you install it separately.
Option 1—Installing the Default JRE/JDK:
One approach to install it is to use the version of Java that comes with the Debian package. Debian 11 is pre-installed with OpenJDK version 11, an open-source JRE and JDK variation compatible with Java 11. Java 11 is the most recent Long Term Support version.
Execute the following command to Install Oracle Java JDK on Debian 11:
The JRE will allow you to run almost all Java software.
Verify the installation with the following:
The following output will return:
openjdk version "11.0.16" 2022-07-19
OpenJDK Runtime Environment (build 11.0.16+8-post-Debian-1deb11u1)
OpenJDK 64-Bit Server VM (build 11.0.16+8-post-Debian-1deb11u1, mixed mode, sharing)
Some Java-based software may require the JDK in addition to the JRE for compilation and operation. Use the following command to install the JDK and the JRE at the same time:
Verify that the JDK is installed by checking the version of javac
, the Java compiler:
You’ll receive the following output:
javac 11.0.16
Next, you will learn how to install Oracle’s official JDK and JRE.
Option 2 — Installing Oracle JDK 11
Oracle’s licensing agreement for Java doesn’t enable automatic installation through package managers. You must first register for an Oracle account, download the JDK manually, then add a new package repository for the version you want to use in order to install the official Oracle JDK. Then, with the aid of an independent installation script, you can utilize apt to install it. The JRE is preinstalled with Oracle JDK, therefore you don’t need to install it separately.
The installation script’s version and the Oracle JDK version you need to download must match. Get the version you require by going to the Oracle Java 11 Installer website. Your package can be found in the following figure:
This image has a script version of 11.0.13. Oracle JDK 11.0.13 is therefore necessary. The version number may vary depending on when you install the app. You don’t need to download anything from this page because you will shortly be receiving the installation script through apt.
Next, go to the Archive Downloads and locate the version that matches the one you need.
From this list, choose the Linux x64 compressed archive .tar.gz
package:
You’ll receive the following output:
gpg: keybox '/usr/share/keyrings/oracle-jdk11-installer.gpg' created
gpg: /tmp/trustdb.gpg: trustdb created
gpg: key EA8CACC073C3DB2A: public key "Launchpad PPA for Linux Uprising" imported
gpg: Total number processed: 1
gpg: imported: 1
Next, add the repository to your list of package sources:
Update your package list to make the new software available for installation:
The installer will look for the Oracle JDK you downloaded in /var/cache/oracle-jdk11-installer-local
. First create this directory:
Then, move the Oracle JDK archive there:
Finally, install the package:
The installer will first ask you to accept the Oracle license agreement. Accept the agreement, and then the installer will extract the Java package and install it.
Now, you will learn how to select the version of Java you want to use.
Step 2—Managing Java
On a single server, several Java installs are possible. Using the update-alternatives command, you can set which version is the default for usage on the command line:
This is the following output if you’ve installed both versions of Java in this tutorial:
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:
Choose the number associated with the Java version to use it as the default, or press ENTER
to leave the current settings in place.
You can do this for other Java commands, such as the compiler (javac
):
Other commands for which this command can be run include, but are not limited to: keytool
, javadoc
, and jarsigner
.
Step 3 — Setting the Environment Variable
Many programs written in Java use the environment variable to determine which Java installation location.
To set this environment variable, first determine where Java is installed. Use the update-alternatives
command:
This output will list each installation of Java along with its installation path:
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
In this case, the installation paths are as follows:
- 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
.
These paths show the path to the java
executable.
Next, copy the path for your preferred installation, excluding the trailing bin/java
component. Then open /etc/environment
using nano
or your preferred text editor:
At first, this file can be empty. Add the following line at the end of the file, being careful to omit the bin/part of the path and substitute your own copied path for the highlighted path:
Environment /etc
JavaHome: “/usr/lib/jvm/java-11-openjdk-amd64”
All users on your system will have their Java_HOME path set if you modify this file.
After saving the file, close the editor. To accomplish this with nano, hit CTRL + X, Y, and then Enter.
To apply the modifications to your current session, refresh this file now:
/etc/environment as the source
Check to see if the environment variable is defined:
$JAVA_HOME echo
You will see the path you previously set in your output:
/usr/lib/jvm/java-11-openjdk-amd64 is the output.
To apply this setting, other users must run the command source /etc/environment or log out and back in.
Conclusion
During this session, you learned how to maintain several Java versions that you installed. Installing Java-based apps such as Cassandra, Glassfish, Tomcat, Jetty, and Jenkins is now possible.
Get more inforamtion about