Java 21 is the latest long-term support (LTS) release from Oracle and the OpenJDK community, with support guaranteed until at least September 2029. If you are running production workloads on RHEL 10, Rocky Linux 10, or AlmaLinux 10, this is the version you should be deploying. It brings virtual threads, pattern matching improvements, and a range of performance gains over Java 17 and earlier releases.
In this guide, I will walk you through two methods to install OpenJDK 21 on RHEL 10-based distributions – using the default AppStream repository and using the Eclipse Temurin (Adoptium) repository. We will also cover setting JAVA_HOME properly, managing multiple Java versions with the alternatives system, and configuring Java for build tools and application servers.
Prerequisites
Before you begin, make sure you have the following in place:
- A running installation of RHEL 10, Rocky Linux 10, or AlmaLinux 10
- Root or sudo access to the server
- An active internet connection for package downloads
- The system should be up to date – run
sudo dnf update -ybefore proceeding
JDK vs JRE – Which One Do You Need?
Before installing, understand the difference between the two main packages:
- JRE (Java Runtime Environment) – contains only the runtime needed to execute Java applications. Install this on servers that only run pre-compiled Java apps like Tomcat, Elasticsearch, or Kafka.
- JDK (Java Development Kit) – includes the JRE plus development tools like
javac(compiler),jdb(debugger), andjar(archive tool). Install this if you need to compile Java code, run Maven or Gradle builds, or do any development work.
My recommendation: install the full JDK unless you are absolutely certain you only need the runtime. The extra disk space is minimal and it saves you from headaches later when a tool expects javac to be present.
Method 1 – Install OpenJDK 21 from AppStream Repository
RHEL 10 and its derivatives ship OpenJDK 21 in the AppStream repository. This is the simplest and most straightforward installation method, and the packages receive security updates through your normal system update process.
Step 1 – Install OpenJDK 21 JDK
To install the full JDK (recommended):
sudo dnf install -y java-21-openjdk-devel
If you only need the JRE:
sudo dnf install -y java-21-openjdk
Step 2 – Verify the Installation
Check the Java runtime version:
java -version
Expected output:
openjdk version "21.0.6" 2025-01-21 LTS
OpenJDK Runtime Environment (Red_Hat-21.0.6.0.7-1) (build 21.0.6+7-LTS)
OpenJDK 64-Bit Server VM (Red_Hat-21.0.6.0.7-1) (build 21.0.6+7-LTS, mixed mode, sharing)
If you installed the JDK, also verify the compiler:
javac -version
Expected output:
javac 21.0.6
Method 2 – Install Eclipse Temurin (Adoptium) from Official Repository
Eclipse Temurin is a production-ready OpenJDK distribution maintained by the Adoptium project. It is a solid alternative if you want builds that are independent of your OS vendor’s release cycle, or if your organization standardizes on Temurin across different platforms.
Step 1 – Add the Adoptium Repository
First, create the repository configuration file:
cat << 'EOF' | sudo tee /etc/yum.repos.d/adoptium.repo
[Adoptium]
name=Adoptium
baseurl=https://packages.adoptium.net/artifactory/rpm/rhel/$releasever/$basearch
enabled=1
gpgcheck=1
gpgkey=https://packages.adoptium.net/artifactory/api/gpg/key/public
EOF
Step 2 – Install Temurin 21 JDK
Install the package:
sudo dnf install -y temurin-21-jdk
Step 3 – Verify the Installation
Check the installed version:
java -version
You should see output similar to:
openjdk version "21.0.6" 2025-01-21 LTS
OpenJDK Runtime Environment Temurin-21.0.6+7 (build 21.0.6+7-LTS)
OpenJDK 64-Bit Server VM Temurin-21.0.6+7 (build 21.0.6+7-LTS, mixed mode, sharing)
Set JAVA_HOME System-Wide
Many Java applications, build tools, and frameworks expect the JAVA_HOME environment variable to be set. The cleanest way to do this system-wide on RHEL-based systems is to create a script in /etc/profile.d/.
Step 1 – Find Your Java Installation Path
Run the following to find where Java is installed:
dirname $(dirname $(readlink -f $(which java)))
For the AppStream OpenJDK package, this will typically return something like:
/usr/lib/jvm/java-21-openjdk-21.0.6.0.7-1.el10.x86_64
Step 2 – Create the JAVA_HOME Profile Script
Create /etc/profile.d/java.sh with the following content:
cat << 'EOF' | sudo tee /etc/profile.d/java.sh
export JAVA_HOME=$(dirname $(dirname $(readlink -f $(which java))))
export PATH=$JAVA_HOME/bin:$PATH
EOF
Make it executable:
sudo chmod +x /etc/profile.d/java.sh
Step 3 – Load and Verify
Source the script to apply it to your current session:
source /etc/profile.d/java.sh
Verify JAVA_HOME is set correctly:
echo $JAVA_HOME
This script will be loaded automatically for all users on their next login.
Manage Multiple Java Versions with Alternatives
If you have more than one Java version installed on the same system – for example, Java 17 for a legacy app and Java 21 for newer workloads – RHEL’s alternatives system lets you switch between them cleanly.
List Available Java Versions
See all installed Java runtimes registered with alternatives:
sudo alternatives --config java
This shows a numbered list of available versions. Enter the number corresponding to the version you want to use as the default, then press Enter.
Switch the Java Compiler
You should also switch the compiler to match:
sudo alternatives --config javac
Verify After Switching
Always verify after making a change:
java -version
javac -version
Both commands should now report the version you selected.
Configure Java for Common Applications
Apache Maven
Maven reads JAVA_HOME to locate the JDK. If you set it in /etc/profile.d/java.sh as described above, Maven will pick it up automatically. Install Maven with:
sudo dnf install -y maven
Verify it detects Java 21:
mvn -version
The output should show Java version: 21.x.x under the runtime information.
Gradle
Gradle also uses JAVA_HOME. If you are installing Gradle manually (not through dnf), download it from the official site and extract it:
wget https://services.gradle.org/distributions/gradle-8.12-bin.zip
sudo mkdir -p /opt/gradle
sudo unzip -d /opt/gradle gradle-8.12-bin.zip
export PATH=/opt/gradle/gradle-8.12/bin:$PATH
Verify:
gradle -version
Check that the JVM line shows Java 21.
Apache Tomcat
Tomcat 10.1.x and 11.x both support Java 21. If you install Tomcat from a tarball, set JAVA_HOME in the Tomcat service file or in /opt/tomcat/bin/setenv.sh:
cat << 'EOF' | sudo tee /opt/tomcat/bin/setenv.sh
export JAVA_HOME=$(dirname $(dirname $(readlink -f $(which java))))
EOF
sudo chmod +x /opt/tomcat/bin/setenv.sh
If you are using a systemd unit file for Tomcat, you can add the environment variable there instead:
[Service]
Environment="JAVA_HOME=/usr/lib/jvm/java-21-openjdk"
Then reload and restart:
sudo systemctl daemon-reload
sudo systemctl restart tomcat
Troubleshooting
java command not found after installation
If you get -bash: java: command not found right after installing, log out and log back in, or source the profile manually:
source /etc/profile.d/java.sh
If the file does not exist yet, check that the java binary is in the PATH:
which java
ls -la /usr/bin/java
Wrong Java version is active
If java -version shows an older version, another Java is taking priority. Check what alternatives are registered:
sudo alternatives --config java
Select the Java 21 entry. Also check if something in your shell profile is overriding the PATH:
which -a java
JAVA_HOME is not set or points to the wrong version
Verify the current value:
echo $JAVA_HOME
If it is empty or incorrect, re-create /etc/profile.d/java.sh as shown in the JAVA_HOME section above, then source it.
javac not found but java works
This means you installed the JRE (java-21-openjdk) but not the JDK. Install the development package:
sudo dnf install -y java-21-openjdk-devel
Then verify:
javac -version
Adoptium repository not working
If dnf fails to fetch packages from the Adoptium repo, check that the repo file is correct:
cat /etc/yum.repos.d/adoptium.repo
Make sure the baseurl contains $releasever (which resolves to 10 on RHEL 10). If Adoptium has not yet published packages for RHEL 10 specifically, you can try using the RHEL 9 path temporarily:
baseurl=https://packages.adoptium.net/artifactory/rpm/rhel/9/$basearch
Then clear the cache and retry:
sudo dnf clean all
sudo dnf install -y temurin-21-jdk
Permission denied errors
All installation commands require root or sudo. If you are running as a non-root user, prefix commands with sudo. If sudo is not configured for your user, ask your system administrator to add you to the wheel group:
sudo usermod -aG wheel yourusername
Summary
You now have Java 21 LTS installed and configured on your RHEL 10, Rocky Linux 10, or AlmaLinux 10 system. Whether you went with the AppStream OpenJDK package or Eclipse Temurin, the end result is a production-ready Java 21 environment. With JAVA_HOME set in /etc/profile.d/java.sh and the alternatives system configured, your Java setup will work correctly with Maven, Gradle, Tomcat, and any other Java-dependent tools you need to run.




























































