How to install Java 8 and 11 on Red Hat Enterprise Linux 8

How to install Java 8 and 11 on Red Hat Enterprise Linux 8

With Red Hat Enterprise Linux (RHEL) 8, two major versions of Java will be supported: Java 8 and Java 11. In this article, I’ll refer to Java 8 as JDK (Java Development Kit) 8 since we are focusing on the development aspect of using Java. JDK 8 and JDK 11 refer to Red Hat builds of OpenJDK 8 and OpenJDK 11 respectively. Through this article, you’ll learn how to install and run simple Java applications on RHEL 8, how to switch between two parallel installed major JDK versions via alternatives and how to select one of the two JDKs on a per-application basis.

TL; DR

To install JDK 8, use:  (If you didn’t  select Make this user administrator during install see this article to enable sudo on RHEL)

$ sudo yum install java-1.8.0-openjdk-devel

Then run Java “Hello World” as follows:

$ cat > HelloWorld.java <<HELLO
public class HelloWorld {
  public static void main(String[] args) {
    System.out.println("Hello World!");
  }
}
HELLO
$ javac HelloWorld.java && java HelloWorld
Hello World!

To instal JDK 11, use:

$ sudo yum install java-11-openjdk-devel

Then run Java “Hello World” as follows:

$ cat > HelloWorld.java <<HELLO
public class HelloWorld {
  public static void main(String[] args) {
    System.out.println("Hello World!");
  }
}
HELLO
$ /usr/lib/jvm/java-11-openjdk/bin/java HelloWorld.java
Hello World!

Yes, with JDK 11 you can directly run Java source files. The compilation step is handled for you.

Watch the Video

If you prefer to watch a short 4 minute demo video, here it is:

 

The Longer Version…

Let’s consider we have a freshly commissioned Red Hat Enterprise Linux 8 machine and we’d like to use it for running Java applications.

OpenJDK logo

Finding available Java packages

In order to figure out which RPM packages to install, we can ask the packaging system which ones provide the java binary:

$ yum provides \*/bin/java

This command tells us that packages java-1.8.0-openjdk-headless and java-11-openjdk-headless both provide the java binary. For the purpose of this article, we’re interested in the development kits, so we’ll install the -devel sub-packages instead. The -devel packages will pull in -headless packages as a dependency. If you already know that RHEL packages are OpenJDK builds, yum list available might be useful too:

$ yum list available \*openjdk\*

For the purpose of this article, we are going to install JDK 8 and JDK 11 in parallel, and also install maven:

$ sudo yum install java-1.8.0-openjdk-devel java-11-openjdk-devel maven

Switching Java Versions

In the previous step, we installed JDK 8 and JDK 11 in parallel. At this point in time, JDK 8 is the main JDK on RHEL 8. That’s why you get this output when running java -version on a fresh RHEL 8 install:

$ java -version
openjdk version "1.8.0_181"
OpenJDK Runtime Environment (build 1.8.0_181-b13)
OpenJDK 64-Bit Server VM (build 25.181-b13, mixed mode)

There are two ways to select the Java version you want:

  1. Switch java and javac binaries system wide via alternatives. This approach requires root privileges.
  2. Select the JDK on a per-application basis by setting JAVA_HOME

Selecting Java Versions with Alternatives

java and javac binaries on RHEL 8 are managed by the alternatives system. This means a system administrator can switch the system java (or javac) to be something other than the default, JDK 8. The alternatives system uses priorities in order to determine which JDK should be available via /usr/bin/java. JDK 8 has been given a higher priority on RHEL-8 than JDK 11. But we are getting ahead of ourselves. First, lets see which binaries are managed by alternatives:

$ alternatives --list

We see that java and javac are managed by alternatives. Next, we are going to switch to JDK 11, using the alternatives --config command:

$ sudo alternatives --config java
There are 2 programs which provide 'java'.
Selection      Command
-----------------------------------------------
*+ 1           java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-8.el8.x86_64/jre/bin/java)
   2           java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.1.13-4.el8.x86_64/bin/java)

Enter to keep the current selection[+], or type selection number: 2

This will switch the system java binary to JDK 11. We do the same for javac since java and javac are independently managed. There is no need to switch anything else as every other JDK binary will switch either with the java or the javac binary:

$ sudo alternatives --config javac
There are 2 programs which provide 'javac'.
Selection      Command
-----------------------------------------------
*+ 1           java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-8.el8.x86_64/jre/bin/javac)
   2           java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.1.13-4.el8.x86_64/bin/javac)

Enter to keep the current selection[+], or type selection number: 2

Switching alternatives non-interactively

Here is an approach to switch to JDK 11, via alternatives, using non-interactive means, which comes in handy if you need to script it:

$ JAVA_11=$(alternatives --display java | grep 'family java-11-openjdk' | cut -d' ' -f1)
$ sudo alternatives --set java $JAVA_11

Similarly, switching to JDK 8 via alternatives by non-interactive means:

$ JAVA_8=$(alternatives --display java | grep 'family java-1.8.0-openjdk' | cut -d' ' -f1)
$ sudo alternatives --set java $JAVA_8

A similar approach can be followed for javac.

Selecting Java Versions by Setting JAVA_HOME

Many applications support using the JAVA_HOME environment variable, as a way to specify which JDK should be used to run the application. The examples below demonstrate this usage when running maven. This approach is handy if you don’t have root privileges, but both JDKs are already installed on your system.

Selecting JDK 8:

$ JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk mvn --version
Apache Maven 3.5.4 (Red Hat 3.5.4-5)
Maven home: /usr/share/maven
Java version: 1.8.0_191, vendor: Oracle Corporation, runtime: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-9.el8.x86_64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.18.0-2.el8.x86_64", arch: "amd64", family: "unix"

Selecting JDK 11:

$ JAVA_HOME=/usr/lib/jvm/java-11-openjdk mvn --version
Apache Maven 3.5.4 (Red Hat 3.5.4-5)
Maven home: /usr/share/maven
Java version: 11.0.1, vendor: Oracle Corporation, runtime: /usr/lib/jvm/java-11-openjdk-11.0.1.13-6.el8.x86_64
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.18.0-2.el8.x86_64", arch: "amd64", family: "unix"

This feature also comes in handy if you have multiple minor JDK versions installed as Leo Ufimtsev described in his article for RHEL 7.

Hello World on Steroids, JDK 11 Style

With JEP 330, part of JDK 11, it’s now possible to run Java code in a script-like fashion. The feature is called “Launch Single-File Source-Code Programs” and allows one to use Java as scripting language. Here is a simple example:

$ cat > factorial <<FACT
#!/usr/lib/jvm/java-11-openjdk/bin/java --source 8
public class Factorial {

  private static void usage() {
    System.err.println("factorial <number>");
    System.exit(1);
  }

  private static long factorial(int num) {
    if (num <= 1) {
      return 1;
    }
    if (num == 2) {
      return 2;
    }
    return factorial(num - 1) * num;
  }

  public static void main(String[] args) {
    if (args.length != 1) {
      usage();
    }
    int num = -1;
    try {
      num = Integer.parseInt(args[0]);
    } catch (Exception e) {
      System.err.println("Error: Argument not a number!");
      usage();
    }
    System.out.println(factorial(num));
  }
}
FACT
$ chmod +x factorial
$ ./factorial 6
720

Thank you. I hope you’ve found this article useful.

Develop using Red Hat's most valuable products

Your membership unlocks Red Hat products and technical training on enterprise cloud application development.

JOIN RED HAT DEVELOPER

Take advantage of your Red Hat Developers membership and download Red Hat Enterprise Linux today at no cost.

Share