Install Java on RHEL, Rocky Linux, AlmaLinux and Fedora

Install Java on RHEL, Rocky Linux, AlmaLinux, CentOS Stream, Oracle Linux, or Fedora using OpenJDK, Eclipse Temurin, or Oracle JDK, then configure alternatives, JAVA_HOME, updates, verification, and uninstall.

Published

Updated

Read time 12 min read

Reviewed byDeepak Prasad

Install Java OpenJDK and Temurin on RHEL-family Linux and Fedora with dnf and alternatives

Java on RHEL, Rocky Linux, AlmaLinux, CentOS Stream, Oracle Linux, and Fedora usually means OpenJDK RPMs from the distribution’s enabled repositories—free, updated through the operating system, and integrated with the alternatives system. When your release does not ship the Java major you need, add Eclipse Temurin from Adoptium or install Oracle JDK from a vendor tarball.

This guide covers OpenJDK with dnf, JRE and headless variants, Temurin on Rocky Linux 10.2, Oracle JDK from a tarball, alternatives version switching, JAVA_HOME, a compile-and-run check, update paths, and uninstall steps per method.

Tested on: Rocky Linux 10.2 (Red Quartz); OpenJDK 21.0.11 (java-21-openjdk-devel); Temurin 17.0.19 (temurin-17-jdk from Adoptium rhel/10 repository).

NOTE
Rocky Linux 10 tested here ships OpenJDK 21 as its default Java implementation. RHEL 10 officially uses OpenJDK 21 as its default Java implementation. RHEL 9 and compatible systems may provide multiple majors, including OpenJDK 8, 11, 17, and 21, depending on the release and enabled repositories. Always query the current host before choosing a package.

JDK, JRE, and package names on RHEL-family Linux

Java terminology overlaps in documentation and RPM names. The table below maps common terms to what you install on Enterprise Linux.

Term What it means When you need it
JVM Java Virtual Machine—the runtime engine that executes bytecode Present with any installed Java package; you rarely install “JVM” as a separate product
JRE Java Runtime Environment—runs compiled Java programs (java) Application servers or services that only execute JARs and WARs and never compile code
JDK Java Development Kit—JRE plus compiler and development tools (javac, headers) Building Java code, Maven, Gradle, JasperReports Buildomatic, or any workflow that compiles
Headless Runtime without graphical UI libraries (AWT/Swing peers) Servers and containers; smallest footprint for backend Java processes
OpenJDK Open-source Java implementation packaged and maintained through your distribution repositories Default choice when the repository supplies the major version your application requires
Temurin Eclipse Adoptium’s tested OpenJDK distribution When your distribution repos do not ship the Java major you need
Oracle JDK Oracle’s JDK distribution When your organization requires Oracle support, licensing terms, or vendor-specific tooling

On RPM-family Linux, package names do not always mirror the major version number literally. Java 8 commonly uses java-1.8.0-openjdk, while newer majors use names such as java-17-openjdk and java-21-openjdk. Search the exact naming your release publishes before you install.

When you are unsure whether you need a JRE or JDK, install the JDK (java-<version>-openjdk-devel). Red Hat OpenJDK documentation recommends the JDK when you are unsure; Fedora includes OpenJDK as its default open-source Java implementation. Graphical export features in applications such as JasperReports Server can still need fonts, a browser, or other dependencies even when the Java process itself runs headless on the server.


Choose an install method

Method Best for Jump to
OpenJDK from dnf Default on RHEL-family Linux—development (-devel) or runtime Method 1
JRE or headless OpenJDK Application servers and containers that only run Java programs Method 2
Eclipse Temurin (Adoptium) Java majors missing from your distribution repos (for example Java 17 on EL10) Method 3
Oracle JDK tarball Oracle support contracts or vendor-specific tooling Method 4

Most readers should install java-<version>-openjdk-devel from their distribution repositories when that version exists. Search the exact package naming used by your release; Java 8 commonly uses java-1.8.0-openjdk, while newer majors use names such as java-17-openjdk and java-21-openjdk. Add Temurin when dnf list available shows no matching OpenJDK package for your target major.


Prerequisites

  • RHEL, Rocky Linux, AlmaLinux, CentOS Stream, Oracle Linux, or Fedora with sudo and outbound HTTPS for dnf and optional Adoptium repositories.
  • Enough disk space—each full JDK is roughly 150–400 MB with dependencies.
  • For Debian-family hosts, see install Java on Debian.

Refresh metadata with dnf makecache before you search or install:

bash
sudo dnf makecache

Check whether Java is already installed:

bash
java -version 2>&1 || echo "java: not installed"
javac -version 2>&1 || echo "javac: not installed"

Sample output when OpenJDK 21 is the active default:

output
openjdk version "21.0.11" 2026-04-21 LTS
OpenJDK Runtime Environment (Red_Hat-21.0.11.0.10-1) (build 21.0.11+10-LTS)
OpenJDK 64-Bit Server VM (Red_Hat-21.0.11.0.10-1) (build 21.0.11+10-LTS, mixed mode, sharing)
javac 21.0.11

List OpenJDK packages your enabled repositories provide:

bash
dnf list available 'java-*-openjdk*'

For a compact package-name list:

bash
dnf repoquery --available --qf '%{name}' 'java-*-openjdk*' | sort -u

Sample output on Rocky Linux 10.2:

output
java-21-openjdk
java-21-openjdk-devel
java-21-openjdk-headless

If repoquery is unavailable, install the DNF plugins package your distribution documents for that release.

List installed OpenJDK packages separately to see what already owns the active alternative:

bash
dnf list installed 'java-*-openjdk*'

On Rocky Linux 10.2 AppStream publishes OpenJDK 21 packages such as those above. RHEL 9 and compatible rebuilds may also list java-17-openjdk, java-11-openjdk, or java-1.8.0-openjdk depending on release and repository access. For broader CLI reference beyond the dnf steps below, see the Linux commands index.


Install the JDK when you compile Java code or run Buildomatic-style tooling. Replace 21 with the major version your application requires and your repositories publish:

bash
sudo dnf install -y java-21-openjdk-devel

The install completes with Complete! when dnf finishes without errors.

Verify the runtime and compiler:

bash
java -version

Sample output:

output
openjdk version "21.0.11" 2026-04-21 LTS
OpenJDK Runtime Environment (Red_Hat-21.0.11.0.10-1) (build 21.0.11+10-LTS)
OpenJDK 64-Bit Server VM (Red_Hat-21.0.11.0.10-1) (build 21.0.11+10-LTS, mixed mode, sharing)
bash
javac -version

Sample output:

output
javac 21.0.11

The java-21-openjdk-devel package pulls in the matching runtime and headless libraries. JVM files land under /usr/lib/jvm/java-21-openjdk/ on the tested host.


Method 2: Install JRE or headless OpenJDK

Use a runtime-only package when you do not need javac—typical for Tomcat hosts that only execute WAR files. Prefer the JDK when you compile code or run Buildomatic-style tooling.

RPM package pattern Meaning Typical use
java-<version>-openjdk-devel JDK with compiler and development tools Maven, Gradle, JasperReports Buildomatic, or any compile step
java-<version>-openjdk Full runtime with additional desktop or GUI-related dependencies Java apps that may use GUI libraries on a desktop-capable host
java-<version>-openjdk-headless Minimal runtime without graphical UI libraries Backend services and containers where only bytecode execution is required

Install a minimal headless runtime:

bash
sudo dnf install -y java-21-openjdk-headless

Confirm only the runtime is required before you skip -devel—tools such as Maven, Gradle, and JasperReports Server Buildomatic need a JDK with javac. Server-side Java can be headless while JasperReports still needs separate browser or font packages for graphical PDF and dashboard export.


Method 3: Install Eclipse Temurin from Adoptium

When your distribution repositories do not ship your target Java major, add the Adoptium RPM repository. Adoptium’s Linux installation docs derive the distribution segment from /etc/os-release and note that unsupported distribution and version combinations may work only on a best-effort basis.

On the tested Rocky Linux 10.2 host, the automatically derived rocky/10 path was unavailable, while the rhel/10 path worked. The repository below therefore pins the tested RHEL-compatible path:

bash
sudo tee /etc/yum.repos.d/adoptium.repo > /dev/null <<'EOF'
[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

On Fedora or other releases, check Adoptium’s current repository tree and supported-platform list before you copy this file. AlmaLinux and CentOS Stream hosts may need the same rhel segment override when the OS ID path does not resolve.

Refresh Adoptium metadata and list available Temurin JDK packages:

bash
sudo dnf makecache --disablerepo='*' --enablerepo=Adoptium
dnf list available 'temurin-*-jdk'

Sample output:

output
temurin-11-jdk.x86_64                 11.0.31.0.0.11-0                  Adoptium
temurin-17-jdk.x86_64                 17.0.19.0.0.10-0                  Adoptium
temurin-21-jdk.x86_64                 21.0.11.0.0.10-0                  Adoptium

Install the major you need—for example Java 17 on Rocky Linux 10 when the tested AppStream repositories had no java-17-openjdk:

bash
sudo dnf install -y temurin-17-jdk

Verify Temurin is registered and check its JVM path:

bash
java -version
ls -ld /usr/lib/jvm/java-17-temurin-jdk

Sample output after switching the default with alternatives (see the next section):

output
openjdk version "17.0.19" 2026-04-21
OpenJDK Runtime Environment Temurin-17.0.19+10 (build 17.0.19+10)
OpenJDK 64-Bit Server VM Temurin-17.0.19+10 (build 17.0.19+10, mixed mode, sharing)
drwxr-xr-x. 9 root root 4096 Jul 12 11:36 /usr/lib/jvm/java-17-temurin-jdk

Fedora hosts may use the fedora Adoptium path from Adoptium Linux installation docs when rhel does not match your release.


Method 4: Install Oracle JDK from a tarball

Oracle JDK is not shipped in distribution repositories on RHEL-family Linux. Review Oracle licensing and support terms before you choose Oracle JDK for organizational deployment. Download the Linux x64 archive from Oracle Java downloads and extract it under /opt with tar:

bash
sudo mkdir -p /opt/jdk
sudo tar -xf jdk-21_linux-x64_bin.tar.gz -C /opt/jdk

Register the binaries with alternatives—replace jdk-21 with your extracted directory name:

bash
sudo alternatives --install /usr/bin/java java /opt/jdk/jdk-21/bin/java 2100
sudo alternatives --install /usr/bin/javac javac /opt/jdk/jdk-21/bin/javac 2100

These commands register only java and javac. A complete JDK exposes additional tools such as jar, javadoc, jshell, and keytool. Register any additional commands your workflow requires, or invoke them through $JAVA_HOME/bin to avoid mixing tools from different JDKs.

Select and verify the Oracle build:

bash
sudo alternatives --config java
sudo alternatives --config javac
java -version
javac -version

Most open-source workloads should prefer OpenJDK or Temurin (OpenJDK install guide).


Switch between installed JDKs

Fedora and RHEL-family packaging use the alternatives system to switch installed Java implementations. When multiple JDK packages are installed, alternatives manages the /usr/bin/java and /usr/bin/javac symlinks separately—selecting Java 17 for java does not necessarily select Java 17 for javac.

List registered JVMs:

bash
alternatives --display java | head -20

Sample output (trimmed):

output
link currently points to /usr/lib/jvm/java-21-openjdk/bin/java
/usr/lib/jvm/java-21-openjdk/bin/java - priority 21001110
/usr/lib/jvm/java-17-temurin-jdk/bin/java - priority 1161

Switch both runtime and compiler interactively:

bash
sudo alternatives --config java
sudo alternatives --config javac

Verify both binaries point at the same major version:

bash
java -version
javac -version
readlink -f "$(command -v java)"
readlink -f "$(command -v javac)"

Or set a specific path—Temurin 17 on the tested host:

bash
sudo alternatives --set java /usr/lib/jvm/java-17-temurin-jdk/bin/java
sudo alternatives --set javac /usr/lib/jvm/java-17-temurin-jdk/bin/javac
java -version
javac -version

Return to distribution OpenJDK 21:

bash
sudo alternatives --set java /usr/lib/jvm/java-21-openjdk/bin/java
sudo alternatives --set javac /usr/lib/jvm/java-21-openjdk/bin/javac
java -version

To return the runtime and compiler to automatic priority-based selection, run:

bash
sudo alternatives --auto java
sudo alternatives --auto javac

Then verify both versions with java -version and javac -version.


Set JAVA_HOME

Build tools and application servers expect JAVA_HOME to point at the JVM root, not the bin/java file. Resolve the active binary with command -v and readlink before you export the path:

bash
readlink -f "$(command -v java)"

Sample output:

output
/usr/lib/jvm/java-21-openjdk/bin/java

Derive and export JAVA_HOME:

bash
export JAVA_HOME="$(dirname "$(dirname "$(readlink -f "$(command -v java)")")")"
echo "$JAVA_HOME"

Sample output:

output
/usr/lib/jvm/java-21-openjdk

Persist JAVA_HOME for login shells:

bash
echo "export JAVA_HOME=$JAVA_HOME" | sudo tee /etc/profile.d/java.sh
sudo chmod 644 /etc/profile.d/java.sh

This stores a fixed JDK path. Changing the java alternative later does not automatically update JAVA_HOME—update the profile file or service configuration when you switch the required JDK. That distinction matters for Tomcat, Maven, Gradle, and JasperReports Buildomatic.

Files under /etc/profile.d/ are loaded by compatible login or interactive shells. A systemd service does not normally source them. For a service, add the variable to its unit or drop-in:

ini
[Service]
Environment="JAVA_HOME=/usr/lib/jvm/java-21-openjdk"

Reload the unit configuration after you edit the file:

bash
sudo systemctl daemon-reload
sudo systemctl restart SERVICE

Replace SERVICE with your unit name, such as tomcat. Some applications also support EnvironmentFile= when they already read a dedicated environment file.

Confirm the directory exists before you hard-code a path in unit files:

bash
ls -ld /usr/lib/jvm/java-21-openjdk*

Verify Java with a sample program

Compile and run a short program to confirm javac and java use the same JDK:

bash
cat > /tmp/Hello.java <<'EOF'
public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello, Java on Rocky Linux");
    }
}
EOF
javac /tmp/Hello.java
java -cp /tmp Hello

Sample output:

output
Hello, Java on Rocky Linux

Remove the test files when you are done:

bash
rm -f /tmp/Hello.java /tmp/Hello.class

Update Java packages

Distribution OpenJDK packages update with normal dnf maintenance:

bash
sudo dnf upgrade 'java-*-openjdk*'

Temurin packages update from the Adoptium repository when it stays enabled:

bash
sudo dnf upgrade 'temurin-*'

Package updates can refresh alternatives registrations or priorities. After updating systems with multiple JDKs, verify the selected runtime and compiler:

bash
java -version
javac -version
alternatives --display java | head -5

Uninstall Java

OpenJDK packages

List installed OpenJDK RPMs with rpm:

bash
rpm -qa | grep openjdk

Remove the development and runtime packages you no longer need. Preview the transaction first:

bash
sudo dnf remove --assumeno java-21-openjdk-devel java-21-openjdk java-21-openjdk-headless

Review the dependency list, then run the actual removal:

bash
sudo dnf remove java-21-openjdk-devel java-21-openjdk java-21-openjdk-headless

After removal, confirm no stale alternative or profile entry remains:

bash
alternatives --display java
java -version

Remove or update /etc/profile.d/java.sh if it still points at the deleted JDK.

Temurin

bash
sudo dnf remove -y temurin-17-jdk
sudo rm -f /etc/yum.repos.d/adoptium.repo
sudo dnf makecache

Oracle tarball

Remove the alternatives entries before you delete the extracted directory:

bash
sudo alternatives --remove java /opt/jdk/jdk-21/bin/java
sudo alternatives --remove javac /opt/jdk/jdk-21/bin/javac
sudo rm -rf /opt/jdk/jdk-21

Replace jdk-21 with your exact extracted directory. Delete any /etc/profile.d/java.sh snippet that referenced that path.


Troubleshooting

Symptom Likely cause Fix
java: command not found No JRE/JDK installed or alternatives broken Install java-<version>-openjdk-headless; run alternatives --display java
javac: command not found JRE only—no JDK Install java-<version>-openjdk-devel
Wrong Java version after install Another JDK has higher alternatives priority, or java and javac differ Run sudo alternatives --config java and sudo alternatives --config javac; verify both with java -version and javac -version
JAVA_HOME unset in builds Shell or service lacks export Set /etc/profile.d/java.sh for login shells; set Environment= or EnvironmentFile= on systemd units
Temurin dnf 404 on Rocky/Alma Wrong Adoptium baseurl distribution segment Check Adoptium current repository tree and supported-platform list; on the tested Rocky Linux 10.2 host, replacing the rocky segment with rhel resolved the repository
No matching Packages to list for Java 17 on EL10 Major not in distribution repositories on the tested host Install temurin-17-jdk from Adoptium instead
GUI Java apps fail on headless server Headless JRE missing AWT peers Install full java-<version>-openjdk or required font/GUI deps

Summary

Install Java on RHEL-family Linux with sudo dnf install java-<version>-openjdk-devel when your distribution repositories ship your target major. Use java-<version>-openjdk-headless for runtime-only hosts when you are certain no compiler is required. Add Eclipse Temurin from Adoptium when the distribution lacks the version you need, or install Oracle JDK from a tarball when your organization requires it. Switch defaults with alternatives for both java and javac, set JAVA_HOME for login shells and systemd separately, verify with java -version and a javac compile test, and preview package removal with dnf remove --assumeno before you uninstall.

Official references:


Frequently Asked Questions

1. How do I install Java on Rocky Linux or RHEL?

Run sudo dnf install -y java-21-openjdk-devel for the JDK from your distribution repositories when that major version exists for your release. Verify with java -version and javac -version. Use alternatives --config java to switch when multiple JDKs are installed.

2. Which Java package should I install for development?

Install java--openjdk-devel. It includes javac and development tools. For running applications only, use java--openjdk or java--openjdk-headless on servers without a GUI stack.

3. How do I install Java 17 on Rocky Linux 10?

Rocky Linux 10 AppStream ships OpenJDK 21 by default and did not list java-17-openjdk in the tested repositories. One option is Eclipse Temurin 17. On the tested Rocky Linux 10.2 host, Adoptium's rocky/10 repository path was unavailable and the rhel/10 path worked; verify the current Adoptium repository tree before applying that workaround.

4. How do I set JAVA_HOME on RHEL-family Linux?

Derive it from the active java binary with export JAVA_HOME="$(dirname "$(dirname "$(readlink -f "$(command -v java)")")")". Persist it in /etc/profile.d/java.sh for login shells. Configure systemd services through Environment=, EnvironmentFile=, or the service supported configuration file.

5. How do I switch between multiple Java versions?

Run sudo alternatives --config java and sudo alternatives --config javac separately, then verify with java -version and javac -version. Selecting Java 17 for java does not automatically select Java 17 for javac.

6. How do I uninstall Java on Rocky Linux?

Remove distribution packages with sudo dnf remove java-21-openjdk-devel and related openjdk packages. For Temurin run sudo dnf remove temurin-17-jdk and disable /etc/yum.repos.d/adoptium.repo when you no longer need that repository.
Omer Cakmak

Linux Administrator

Highly skilled at managing Debian, Ubuntu, CentOS, Oracle Linux, and Red Hat servers. Proficient in bash scripting, Ansible, and AWX central server management, he handles server operations on …