Java is the most widely deployed programming language in enterprise environments, powering banking systems, e-commerce platforms, microservices, Android applications, and big data frameworks like Apache Kafka, Hadoop, and Spark. OpenJDK (Open Java Development Kit) is the open-source reference implementation of the Java SE specification, maintained by Red Hat, IBM, and the Java community. RHEL 9 provides OpenJDK directly from the Red Hat AppStream repository with full support and security patching. Multiple Java versions can be installed simultaneously and switched using the alternatives system. This guide covers installing OpenJDK 21 LTS (the current long-term support release) on RHEL 9, configuring JAVA_HOME, managing multiple JDK versions, and verifying the installation.

Prerequisites

  • RHEL 9 with sudo/root access

Step 1 — Check Available OpenJDK Versions

dnf search java | grep -E "java-[0-9]+-openjdk "

# Available versions in RHEL 9 AppStream:
# java-17-openjdk       (LTS — supported until 2026)
# java-21-openjdk       (LTS — supported until 2028)
# java-11-openjdk       (LTS — older but still available)

Step 2 — Install OpenJDK 21

# JRE only (for running Java applications)
dnf install -y java-21-openjdk

# Full JDK (includes compiler, javadoc, debugger — required for development)
dnf install -y java-21-openjdk-devel

java -version
javac -version

Step 3 — Configure JAVA_HOME

# Find the JDK installation path
dirname $(readlink -f $(which java))

# Set JAVA_HOME system-wide
cat >> /etc/profile.d/java.sh << 'EOF'
export JAVA_HOME=$(dirname $(dirname $(readlink -f $(which java))))
export PATH=$PATH:$JAVA_HOME/bin
EOF

source /etc/profile.d/java.sh
echo $JAVA_HOME

Step 4 — Manage Multiple JDK Versions

# Install both JDK 17 and JDK 21
dnf install -y java-17-openjdk-devel java-21-openjdk-devel

# List available Java alternatives
alternatives --list | grep java

# Switch default Java version interactively
alternatives --config java

# Or set directly (check the exact path with 'alternatives --list')
alternatives --set java /usr/lib/jvm/java-21-openjdk-21.x.x.x.el9.x86_64/bin/java

java -version

Step 5 — Verify JVM and JDK Tools

java -version
javac -version
jar --version
keytool -help | head -5
jshell --version  # Interactive Java REPL (JDK 9+)

Step 6 — Configure JVM Memory Limits

# Test JVM ergonomics — shows auto-detected heap size
java -XX:+PrintFlagsFinal -version 2>&1 | grep -E "MaxHeapSize|InitialHeapSize"

# Override JVM heap size via environment (application-specific)
export JAVA_OPTS="-Xms512m -Xmx2g -XX:+UseG1GC"

# For containerised environments:
# java -XX:MaxRAMPercentage=75 ... (use 75% of container RAM as max heap)

Conclusion

OpenJDK 21 on RHEL 9 is the current Java LTS release with virtual threads (Project Loom), record patterns, and significant performance improvements over Java 17. The alternatives system allows maintaining multiple JDK versions for different applications — useful when running legacy applications that require Java 11 or 17 alongside new applications on Java 21. Always install the -devel package for development environments; the base java-21-openjdk package installs only the JRE and cannot compile Java source code.

Next steps: How to Install Maven and Gradle on RHEL 9, How to Deploy a Spring Boot Application on RHEL 9, and How to Install Docker on RHEL 9.