Last Updated on 4 weeks by Sachin G

As someone who manages multiple Linux servers daily, keeping them Java-ready is part of my routine. Recently, I had to install the latest Java version across CentOS Stream, AlmaLinux, or Rocky Linux machines for a project. Here’s my detailed experience, including real terminal commands, a few hiccups, and what worked best.

Java JDK is a widely used programming language, known for its versatility and compatibility across various applications. Keeping Java up-to-date on your Red Hat Enterprise Linux (RHEL), CentOS, Fedora, AlmaLinux, Rocky, Oracle or Linux-based operating system is crucial for security and performance reasons.

In this article, we’ll walk you through how to install the latest Java on RHEL-based systems using two methods:

Via YUM repository (automated)

Manual Java setup using tar.gz binaries

We’ll also cover common post-installation steps like setting JAVA_HOME and choosing between OpenJDK and Oracle Java.

My Experience Using Java JDK on RHEL-Based Distros

I started with AlmaLinux 9 Version —part of the RPM-based Linux family, just like CentOS Stream and Rocky Linux. I’ve always preferred OpenJDK over Oracle’s version due to its ease of setup and open licensing. That said, I’ll also touch on OpenJDK vs Oracle JDK below.

 
Operating System: Fedora Linux 37 (Workstation Edition)
Kernel: Linux 6.5.5-100.fc37.x86_64
Architecture: x86-64

Method 1: Install Java on AlmaLinux/CentOS/Rocky Using YUM (Recommended for Beginners)

Using the YUM Java repository is the easiest and cleanest method for most users.

Step-by-Step Guide to Install Java on RHEL-Based Systems

Before installing Java via the YUM repository, it’s important to start by searching for and inspecting the available information using a Yum or dnf tool. This information will show which version of Java will be installed through the ‘java-latest-openjdk’ package. Subsequently, you can access the Java RPM package in the application stream repository and utilize either the ‘dnf’ or ‘yum’ command in a Linux environment for installation.”

# dnf search openjdk 

Now, list and install the latest version of Java install.

# dnf list java-latest-openjdk 
# dnf info java-latest-openjdk 
# dnf install java-latest-openjdk 

In the ‘dnf search’ results, you will find various available versions of the JDK. If you have a specific version in mind that you wish to install, simply choose the name of that particular version from the list and proceed with the installation.

To verify the installation, you can execute the below command :

# java --version 
openjdk 21 2023-09-19
OpenJDK Runtime Environment (Red_Hat-21.0.0.0.35-1.rolling.fc37) (build 21+35)
OpenJDK 64-Bit Server VM (Red_Hat-21.0.0.0.35-1.rolling.fc37) (build 21+35, mixed mode, sharing)

Set Default Java jdk Version :

If you have installed different Java versions installed on your Linux system, you can set the default Java version using the alternatives command. This will allow you to switch between different Java versions easily.

To list the installed Java versions, use:

# alternatives --list 

To set a default Java version, use:

# alternatives -config java 

Common Real-World Use Case:

A DevOps engineer managing microservices on Rocky Linux Project chooses the YUM method to deploy OpenJDK across multiple containers quickly, ensuring uniformity and speed of deployment.

Method 2: Manual Java Setup Using tar.gz Binaries (Advanced Users)

This method offers full control, especially when the required Java version isn’t available in YUM

Download Java tar.gz from Oracle or Adoptium.

Download the latest  Java SE  Development Kit from its official Java vendor website. Below are the steps to extract and download JDK files in a temporary folder in Linux. You have to download the tar archive file. We downloaded the JDK file in our temp folder. We will go into the /tmp folder and extract it there.

OpenJDK JDK binaries are available for Linux on jdk.java.net as compressed zip or tar.gz archives.

# cd /tmp
# wget https://download.oracle.com/java/21/latest/jdk-21_linux-x64_bin.tar.gz
# tar xzf jdk-21_linux-x64_bin.tar.gz
# mv jdk-21 /opt

Install  Java 21

Now we have got extracted folder of Java. The alternatives command is the best way to install Java. Go to the extracted folder of Java and run like below command as shown.

# cd /opt/jdk-21

# alternatives --install /usr/bin/java java /opt/jdk-21/bin/java 2 

Now we should set the jar and javac  path

# alternatives --install /usr/bin/jar jar /opt/jdk-21/bin/jar 2

# alternatives --install /usr/bin/javac javac /opt/jdk-21/bin/javac 2

# alternatives --install /usr/bin/javac javac /opt/jdk-21/bin/javac 2

# alternatives --set javac /opt/jdk-21/bin/javac

Set  Java Environment Variables :

After the set of Javac and JRE paths, we will set the environment variables in Linux. Many application needs to use the JAVA_Home or JRE_HOME environment variables for the Java executable to use.

Set the java jdk home variable using the below command.

export JAVA_HOME=/opt/jdk-21

Path set  jre home variable use below command

export JRE_HOME=/opt/jdk-21/jre/

Setting the path variables use the below command

export PATH=$PATH:/opt/jdk-21/bin:/opt/jdk-21/jre/bin

Verify Installed Java JDK Version

Now check the version of installed Java on the Linux box through the below command

java 21 2023-09-19 LTS
Java(TM) SE Runtime Environment (build 21+35-LTS-2513)
Java HotSpot(TM) 64-Bit Server VM (build 21+35-LTS-2513, mixed mode, sharing)

if the java command not running after setting the environment variables, you can add the below lines in bashrc and profile files in the /etc directory.

JAVA_HOME=/opt/jdk-21/
export JRE_HOME=/opt/jdk-21/jre
export PATH=$JAVA_HOME/bin:$PATH

Installing the latest Java version on RPM-based Linux systems like CentOS, AlmaLinux, and Rocky Linux doesn’t have to be complex. Thanks to the YUM install Java command (or DNF on newer versions), it’s a relatively painless process, especially if you stick to OpenJDK.

OpenJDK vs Oracle Java: What’s the Difference?

FeatureOpenJDKOracle Java
LicenseOpen Source (GPL)Oracle Binary Code License
CostFreeFree for dev, license for prod
SupportCommunity-supportedCommercial support available
PerformancePractically identicalOptimized builds available

For most use cases, OpenJDK is more than sufficient — especially for development, scripting, and container workloads.

Difference Between YUM and Binary Java Installation on Linux

YUM Installation:

  • Easier, recommended for most users
  • Automatically manages PATH
  • Integrated with system package manager

Binary Installation:

  • Ideal for multiple Java versions
  • Gives complete control
  • Needs manual environment setup

FAQ – Java Installation on CentOS, AlmaLinux & Rocky Linux

Q1: How do I set JAVA_HOME on AlmaLinux or Rocky Linux?

Create a script in /etc/profile.d/java.sh with:

export JAVA_HOME=/opt/java
export PATH=$JAVA_HOME/bin:$PATH

Q2: Is OpenJDK safe for production?


Absolutely. It’s open source, stable, and widely adopted by enterprises including AWS, Red Hat, and Google.

Q3: Which Linux version is best for Java development?


CentOS Stream, AlmaLinux, and Rocky Linux all provide stability, RHEL-compatibility, and a strong community.