Table of Contents#
- Introduction
- Prerequisites
- Installation Methods
- Verifying the Installation
- Setting the JAVA_HOME Environment Variable
- Uninstalling Java 17
- Conclusion
- References
Prerequisites#
Before starting, ensure your system meets the following requirements:
- A RHEL-based Linux distribution (RHEL 8+, CentOS Stream 8+, Fedora 34+, Rocky Linux 8+, AlmaLinux 8+, etc.).
- sudo privileges: You need administrative access to install packages.
- Internet connection: To download packages and updates.
- Updated system: It’s recommended to update your system packages first to avoid conflicts:
sudo dnf update -y
Installation Methods#
Method 1: Install Oracle JDK 17#
Oracle JDK is the official distribution from Oracle. It includes commercial features and requires a license for production use (free for development and testing).
Step 1: Download the Oracle JDK 17 RPM Package#
-
Visit the Oracle JDK 17 Download Page.
-
Accept the license agreement ("Oracle Technology Network License Agreement for Oracle Java SE").
-
Download the RPM package for Linux (e.g.,
jdk-17.0.9_linux-x64_bin.rpm—version numbers may vary).Note: Oracle requires accepting the license and may require authentication. You can either:
- Download manually in your browser after accepting the license and logging in, then transfer the file to your server.
- Or use
wget/curlwith the appropriate cookie (obtained from your browser after accepting the license) to download directly:
wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" https://download.oracle.com/java/17/latest/jdk-17_linux-x64_bin.rpm
Step 2: Install the RPM Package#
Once downloaded, install the RPM with rpm:
sudo rpm -ivh jdk-17_linux-x64_bin.rpm -i: Install the package.-v: Verbose output.-h: Show a progress bar.
Oracle JDK will install to /usr/java/jdk-17.x.x/ (e.g., /usr/java/jdk-17.0.9/).
Method 2: Install OpenJDK 17 (Recommended)#
OpenJDK is the open-source implementation of Java. It’s free, open, and suitable for most use cases (no licensing restrictions). Most RHEL-based distributions include OpenJDK 17 in their default repositories.
Step 1: Search for OpenJDK 17 Packages#
Check if OpenJDK 17 is available in your repo:
sudo dnf search openjdk-17 You’ll see packages like:
java-17-openjdk: JRE (Java Runtime Environment) for running Java apps.java-17-openjdk-devel: JDK (Java Development Kit) for developing Java apps (includesjavac,jar, etc.).
Step 2: Install OpenJDK 17#
Install the JDK (recommended for development) or JRE (for runtime only):
-
Install JDK (development):
sudo dnf install java-17-openjdk-devel -y -
Install JRE (runtime only):
sudo dnf install java-17-openjdk -y
OpenJDK installs to /usr/lib/jvm/java-17-openjdk-17.0.9.0.9-1.el9.x86_64/ (path varies by version and distro).
Verifying the Installation#
After installation, verify Java is working with:
Check Java Version#
java -version Expected Output (Oracle JDK):
java version "17.0.9" 2023-10-17 LTS
Java(TM) SE Runtime Environment (build 17.0.9+9-LTS-219)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.9+9-LTS-219, mixed mode, sharing)
Expected Output (OpenJDK):
openjdk version "17.0.9" 2023-10-17 LTS
OpenJDK Runtime Environment (Red_Hat-17.0.9.0.9-1.el9) (build 17.0.9+9-LTS)
OpenJDK 64-Bit Server VM (Red_Hat-17.0.9.0.9-1.el9) (build 17.0.9+9-LTS, mixed mode, sharing)
Check the Java Compiler (JDK Only)#
If you installed the JDK, verify javac (Java compiler) works:
javac -version Expected Output:
javac 17.0.9
Setting the JAVA_HOME Environment Variable#
Many applications (e.g., Tomcat, Maven, Jenkins) require the JAVA_HOME environment variable to locate the Java installation.
Step 1: Find the Java Installation Path#
Use update-alternatives to list installed Java versions and their paths:
sudo update-alternatives --config java Example output:
There is 1 program that provides 'java'.
Selection Command
-----------------------------------------------
*+ 1 /usr/java/jdk-17.0.9/bin/java
Enter to keep the current selection[+], or type selection number:
Copy the path (e.g., /usr/java/jdk-17.0.9/ for Oracle JDK or /usr/lib/jvm/java-17-openjdk-17.0.9.0.9-1.el9.x86_64/ for OpenJDK).
Step 2: Set JAVA_HOME Globally#
To set JAVA_HOME for all users, edit /etc/profile (system-wide) or ~/.bashrc (user-specific).
System-Wide (All Users):#
sudo nano /etc/profile Add the following line at the end (replace the path with your Java installation path):
export JAVA_HOME="/usr/java/jdk-17.0.9/"
export PATH="$JAVA_HOME/bin:$PATH" Save and exit (Ctrl+O, Enter, Ctrl+X).
User-Specific (Current User Only):#
nano ~/.bashrc Add the same lines as above, then save and exit.
Step 3: Apply the Changes#
Reload the profile to apply JAVA_HOME:
source /etc/profile # For system-wide changes
# OR
source ~/.bashrc # For user-specific changes Step 4: Verify JAVA_HOME#
echo $JAVA_HOME Output should show your Java path (e.g., /usr/java/jdk-17.0.9/).
Uninstalling Java 17#
Uninstall Oracle JDK 17#
sudo rpm -e jdk-17-17.0.9-1.el8.x86_64 # Replace with your package name Check installed Oracle JDK packages first with:
rpm -qa | grep jdk Uninstall OpenJDK 17#
sudo dnf remove java-17-openjdk-devel -y # For JDK
# OR
sudo dnf remove java-17-openjdk -y # For JRE Conclusion#
You’ve learned two methods to install Java 17 on RHEL-based systems: Oracle JDK (official, licensed) and OpenJDK (open-source, recommended for most users). Always verify the installation with java -version and set JAVA_HOME for applications that require it.
For development or production use without licensing concerns, OpenJDK is the best choice. Oracle JDK is ideal if you need commercial features or support from Oracle.