Install Java 11 (OpenJDK 11) on RHEL 8|CentOS 8|Rocky Linux 8

This article will help you to install Oracle Java 11 on RHEL 8/CentOS 8/Rocky Linux 8. Java 11 is a long-term support (LTS) release which was made available to the General public on 25 September 2018 and is production-ready.

For Java 8 installation, use how to install Java 8 on RHEL / CentOS / Rocky Linux 8.

There are two ways of installing Java 11 on RHEL 8 / CentOS 8 / Rocky Linux 8.

  1. Install Java SE Development Kit 11 (JDK 11)
  2. Install OpenJDK 11

It is recommended to install OpenJDK 11 because of the license issues around Java SE Development Kit. This guide will cover the installation of both.

For Ubuntu use: How to Install Java 11 on Ubuntu

For CentOS 7: Install Java 11 on CentOS 7 / Fedora

Install OpenJDK 11 on RHEL 8|CentOS 8|Rocky Linux 8

OpenJDK is a free and open-source implementation of the Java Platform, Standard Edition licensed under the GNU General Public License version 2.

sudo yum -y install java-11-openjdk java-11-openjdk-devel

Once OpenJDK 11 is installed, confirm it works by checking the version.

$ java -version
openjdk version "11.0.14" 2022-01-18 LTS
OpenJDK Runtime Environment 18.9 (build 11.0.14+9-LTS)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.14+9-LTS, mixed mode, sharing)

$ which java
/usr/bin/java

Set Java environment variables

sudo tee /etc/profile.d/java11.sh <<EOF
export JAVA_HOME=\$(dirname \$(dirname \$(readlink \$(readlink \$(which javac)))))
export PATH=\$PATH:\$JAVA_HOME/bin
export CLASSPATH=.:\$JAVA_HOME/jre/lib:\$JAVA_HOME/lib:\$JAVA_HOME/lib/tools.jar
EOF

Source the file to start using it without logging out.

source /etc/profile.d/java11.sh

Install Java SE Development Kit 11 (JDK 11) on RHEL 8 / CentOS 8 / Rocky Linux 8

Download the latest release of JDK 11.

$ file jdk-11.0.15.1_linux-x64_bin.rpm
jdk-11.0.15.1_linux-x64_bin.rpm: RPM v3.0 bin i386/x86_64 jdk-11-2000:11.0.15.1-ga

Then install the package with the rpmcommand

$ sudo rpm -Uvh jdk-11.*_linux-x64_bin.rpm
warning: jdk-11.0.15.1_linux-x64_bin.rpm: Header V3 RSA/SHA256 Signature, key ID ec551f03: NOKEY
Verifying...                          ################################# [100%]
Preparing...                          ################################# [100%]
Updating / installing...
   1:jdk-11-2000:11.0.15.1-ga         ################################# [100%]

Confirm Java version installed

$ java -version
java version "11.0.15.1" 2022-04-22 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.15.1+2-LTS-10)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.15.1+2-LTS-10, mixed mode)

Test Java installation on RHEL 8 / CentOS 8 / Rocky Linux 8

Write a Hello World Java code to test.

 cat > hello_world.java <<EOF
public class helloworld {
  public static void main(String[] args) {
    System.out.println("Hello Java World!");
  }
}
EOF

Run the code.

$ java hello_world.java
Hello Java World!

Selecting Java Versions with Alternatives

If you have other versions of JDK installed, you set your default Java as follows.

sudo alternatives --list
sudo alternatives --config java

Select Java version to use as default.

install java 11 rhel 8 select java

This will switch the system java binary to selected Java. Also do the same for  javac since java and javac are independently managed.

sudo alternatives --config javac