How to Install Java (OpenJDK) on RHEL 7
Java remains one of the most widely used programming languages and runtime environments in enterprise environments. Whether you are running a web application server, building microservices, or working with big data tools like Hadoop or Kafka, Java is almost certainly part of your stack. Red Hat Enterprise Linux 7 ships with OpenJDK packages available directly in its repositories, making installation straightforward. This guide walks you through installing both the Java Runtime Environment (JRE) and Java Development Kit (JDK) on RHEL 7, managing multiple Java versions with the alternatives system, and configuring the JAVA_HOME environment variable for system-wide access.
Prerequisites
- A running RHEL 7 system with root or
sudoaccess - An active subscription or access to RHEL 7 package repositories (or a configured local mirror)
- Basic familiarity with the command line
Step 1: Check for Existing Java Installations
Before installing, check whether Java is already present on the system. This avoids duplicate installations and helps you understand the current state of your environment.
java -version
rpm -qa | grep java
rpm -qa | grep jdk
If Java is not installed, those commands will return nothing or a “command not found” error. If older or conflicting versions are present, you may want to remove them first with yum remove before proceeding, or use the alternatives tool covered later to manage coexisting versions.
Step 2: Search Available OpenJDK Packages
RHEL 7 provides OpenJDK packages in its default repositories. Use yum search to list what is available before installing.
yum search openjdk
You will typically see output listing packages such as:
java-1.8.0-openjdk.x86_64
java-1.8.0-openjdk-devel.x86_64
java-1.8.0-openjdk-headless.x86_64
java-11-openjdk.x86_64
java-11-openjdk-devel.x86_64
java-11-openjdk-headless.x86_64
The headless variant installs only the JRE without graphical libraries — useful for servers with no display. The devel package includes the full JDK with the compiler (javac), debugger, and header files needed for development.
Step 3: Install Java 8 (OpenJDK 1.8)
Java 8 remains widely used in enterprise settings due to its long-term support and broad application compatibility. To install the full JDK (which includes the JRE):
sudo yum install -y java-1.8.0-openjdk java-1.8.0-openjdk-devel
Confirm the installation succeeded:
java -version
Expected output:
openjdk version "1.8.0_412"
OpenJDK Runtime Environment (build 1.8.0_412-b08)
OpenJDK 64-Bit Server VM (build 25.412-b08, mixed mode)
Check the compiler is available:
javac -version
javac 1.8.0_412
Step 4: Install Java 11 (OpenJDK 11)
Many modern frameworks — including Spring Boot 3.x and newer Jakarta EE releases — require Java 11 or later. Install it alongside Java 8:
sudo yum install -y java-11-openjdk java-11-openjdk-devel
Verify the package is installed:
rpm -q java-11-openjdk-devel
Step 5: Managing Multiple Java Versions with alternatives
RHEL 7 uses the alternatives system (part of chkconfig) to manage multiple versions of the same command. When more than one JDK is installed, you can switch between them without modifying paths manually.
List available Java alternatives:
sudo alternatives --config java
You will see a numbered menu similar to:
There are 2 programs which provide 'java'.
Selection Command
-----------------------------------------------
*+ 1 /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.412.b08-2.el7_9.x86_64/jre/bin/java
2 /usr/lib/jvm/java-11-openjdk-11.0.23.0.9-2.el7_9.x86_64/bin/java
Enter to keep the current selection[+], or type selection number:
Type the number corresponding to your desired version and press Enter. Do the same for javac when the JDK is installed:
sudo alternatives --config javac
Step 6: Set the JAVA_HOME Environment Variable
Many Java-based tools — including Maven, Gradle, Tomcat, and Jenkins — rely on the JAVA_HOME environment variable. The recommended approach on RHEL 7 is to create a dedicated script in /etc/profile.d/ so that all users and login shells pick it up automatically.
First, find the actual JVM installation path. The alternatives symlink points to the active JDK:
readlink -f /usr/bin/java
This returns something like:
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.412.b08-2.el7_9.x86_64/jre/bin/java
Strip the trailing /jre/bin/java (or /bin/java for Java 11) to get the JAVA_HOME base directory. Then create the profile script:
sudo tee /etc/profile.d/java.sh <<'EOF'
# Set JAVA_HOME for all users
export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.412.b08-2.el7_9.x86_64
export PATH=$JAVA_HOME/bin:$PATH
EOF
Apply the changes in your current shell without logging out:
source /etc/profile.d/java.sh
Verify the variable is set correctly:
echo $JAVA_HOME
$JAVA_HOME/bin/java -version
Step 7: Difference Between JRE and JDK
Understanding the distinction helps you install only what is needed on each machine:
- JRE (Java Runtime Environment) — contains the Java Virtual Machine (JVM) and standard class libraries needed to run compiled Java programs. Installed by
java-1.8.0-openjdkorjava-11-openjdk. Use this on production servers that only need to run pre-built JARs or WARs. - JDK (Java Development Kit) — includes the JRE plus the compiler (
javac), documentation tools (javadoc), debugger (jdb), and header files for native development. Installed by the-develpackages. Required on build servers and developer workstations.
For a CI/CD build agent or developer workstation, always install the -devel package. For a production application server that receives pre-built artifacts, the base JRE package is sufficient and reduces the attack surface.
Step 8: Verify the Full Installation
Run a quick end-to-end check to confirm everything is in order:
# Check runtime version
java -version
# Check compiler version (JDK only)
javac -version
# Check JAVA_HOME
echo $JAVA_HOME
# Confirm the alternatives link
ls -la /usr/bin/java
# List all installed Java packages
rpm -qa | grep openjdk
Conclusion
Installing Java on RHEL 7 is a straightforward process thanks to the OpenJDK packages available in the distribution’s default repositories. By using yum to install both Java 8 and Java 11, leveraging the alternatives system to switch between them cleanly, and configuring JAVA_HOME through a /etc/profile.d/ script, you have a robust, maintainable Java environment that works for both development and production workloads. With this foundation in place, you are ready to install build tools like Maven and Gradle or deploy Java application servers on your RHEL 7 system.