Easy Steps To Install OpenJDK 17 on Debian 11 – OrcaCore
This tutorial will guide you through the process of installing OpenJDK 17 on Debian 11. OpenJDK is a robust, free, and open-source implementation of the Java platform. It serves as a viable alternative to Oracle JDK, offering comparable performance without the licensing costs.
In this article from Orcacore, you’ll learn how to download Java 17, configure the environment path on your Debian 11 system, and verify your installation by creating and running a simple Java program. We’ll make installing OpenJDK 17 on Debian 11 straightforward.
Note: We also have guides for installing Java on other operating systems:
- Set up OpenJDK 17 on Ubuntu 20.04
- How To Install OpenJDK 17 on AlmaLinux 9
- How To Install OpenJDK 17 on Centos 7
To follow this guide, you’ll need access to a Debian 11 server with a non-root user that has sudo privileges. If you haven’t already, you can follow our guide on Initial Server Setup with Debian 11 to set this up. Now, let’s get started with the OpenJDK 17 on Debian 11 installation.
Step 1 – Download OpenJDK 17 Binary Package
First, update your system’s package list:
sudo apt update
Next, visit the JDK Downloads page to download the latest binary package for Java 17. Use the wget
command to download the appropriate file:
sudo wget https://download.java.net/java/GA/jdk17.0.2/dfd4a8d0985749f896bed50d7138ee7f/8/GPL/openjdk-17.0.2_linux-x64_bin.tar.gz
Extract the downloaded archive:
sudo tar xvf openjdk-17.0.2_linux-x64_bin.tar.gz
Move the extracted directory to the /opt
directory:
sudo mv jdk-17.0.2 /opt/
Step 2 – Configure Java 17 Environment Path on Debian 11
Now, you need to configure the JAVA_HOME
environment variable. Create a new shell script in /etc/profile.d/
to set these variables:
sudo tee /etc/profile.d/jdk17.sh <<EOF
export JAVA_HOME=/opt/jdk-17.0.2
export PATH=$PATH:$JAVA_HOME/bin
EOF
Apply the changes to your current session:
source /etc/profile.d/jdk17.sh
Verify that the JAVA_HOME
variable is set correctly:
echo $JAVA_HOME
Output
/opt/jdk-17.0.2
Confirm that Java 17 is installed correctly by checking the Java version:
java -version
Step 3 – Test Java 17 Installation on Debian 11
Let’s create a simple "Hello, World" program to ensure Java is working as expected.
Create a file named HelloWorld.java
using your favorite text editor (here, we’ll use vi
):
vi HelloWorld.java
Add the following Java code to the file:
public class HelloWorld {
public static void main(String[] args) {
// Prints "Hello, World" to the terminal window.
System.out.println("Hello, World");
}
}
Save and close the file.
Compile and run the Java code:
java HelloWorld.java
Output
Hello, World
That’s it! You’ve successfully installed and tested Java 17 on your Debian 11 system.
Conclusion
You’ve successfully downloaded the JDK 17 binary package, configured the environment path, and tested your installation. You can now develop and run Java applications on your Debian 11 server.
You might also be interested in:
FAQs
What is OpenJDK?
It is an open-source Java Platform, Standard Edition (Java SE) implementation. It provides all the tools required to develop and run Java applications.
How do I verify that OpenJDK 17 has been installed?
You check your Java version: java -version
How do I set OpenJDK 17 as the default Java version?
You can use the following command to configure the default Java version:sudo update-alternatives --config java
Then, select OpenJDK 17 from the options.
Where is OpenJDK 17 installed on Debian 11?
By default, OpenJDK is installed in /usr/lib/jvm
.
Alternative Solutions for Installing OpenJDK 17 on Debian 11
While the previous method details a manual installation process, there are alternative, often simpler, approaches to installing OpenJDK 17 on Debian 11. Here are two different ways:
1. Using apt
Package Manager (The Recommended Approach)
Debian’s apt
package manager offers a more streamlined and maintainable way to install OpenJDK. This method handles dependencies automatically and simplifies updates.
Explanation:
Debian’s repositories contain pre-built packages for OpenJDK. Using apt
, you can install these packages with a single command. apt
also manages dependencies, ensuring that all necessary libraries and tools are installed. Furthermore, when updates are available, apt
can easily upgrade your OpenJDK installation to the latest version. This method is often preferred because it integrates well with the system’s package management and provides automatic updates.
Steps:
-
Update the package index:
sudo apt update
-
Install OpenJDK 17:
sudo apt install openjdk-17-jdk
This command installs both the Java Development Kit (JDK) and the Java Runtime Environment (JRE) for OpenJDK 17.
-
Verify the installation:
java -version
The output should confirm that OpenJDK 17 is installed.
-
Setting the default Java version (if needed):
If you have multiple Java versions installed, you might want to set OpenJDK 17 as the default:
sudo update-alternatives --config java
This command will present a list of installed Java versions, allowing you to select OpenJDK 17.
Advantages:
- Simpler and faster installation process.
- Automatic dependency management.
- Easy updates through
apt
. - Integrates seamlessly with the Debian system.
2. Using SDKMAN! (Software Development Kit Manager)
SDKMAN! is a tool for managing multiple software development kits (SDKs) on a single system. It simplifies the installation, switching, and removal of different SDK versions, including OpenJDK.
Explanation:
SDKMAN! provides a command-line interface for downloading, installing, and managing various SDKs. It handles the complexities of downloading the correct binaries, setting up environment variables, and switching between different SDK versions. This is particularly useful if you need to work with multiple Java versions simultaneously.
Steps:
-
Install SDKMAN!:
curl -s "https://get.sdkman.io" | bash
Follow the on-screen instructions to complete the installation. You may need to open a new terminal or source your shell configuration file (e.g.,
source ~/.bashrc
orsource ~/.zshrc
) for SDKMAN! to be available. -
Install OpenJDK 17:
sdk install java 17-open
SDKMAN! will download and install OpenJDK 17. You can also specify a particular distribution (e.g.,
sdk install java 17.0.2-tem
) to install a specific build. Typesdk list java
for a complete list of available distributions. -
Set OpenJDK 17 as the default:
sdk use java 17-open
This command sets OpenJDK 17 as the active Java version for your current terminal session. To make it the default for all future sessions, use:
sdk default java 17-open
-
Verify the installation:
java -version
Code Example (SDKMAN! specific):
To list all available Java versions that can be installed via SDKMAN!:
sdk list java
This command will output a list of available Java distributions and their identifiers, which can be used with the sdk install
command.
Advantages:
- Easy management of multiple SDK versions.
- Simple installation and switching between SDKs.
- Support for a wide range of SDKs, not just Java.
- Provides isolated environments for different projects.
Disadvantages:
- Requires installing and configuring SDKMAN!.
- Adds another layer of abstraction.
By using apt
or SDKMAN!, you can greatly simplify the process of installing and managing OpenJDK 17 on your Debian 11 system, compared to the manual installation approach detailed in the first part of this article. The apt method is recommended if you need a simple, system-integrated approach, while SDKMAN! is better suited if you work with multiple SDKs or need to manage different Java versions for various projects.