Red hat enterprise linux java

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

Perl in RHEL 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 $ javac HelloWorld.java && java HelloWorld Hello World!
$ sudo yum install java-11-openjdk-devel

Then run Java “Hello World” as follows:

$ cat > HelloWorld.java > 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.

Читайте также:  Javascript кнопка выбрать все

Video demo

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.

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:

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:

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.

$ 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"
$ 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 "); System.exit(1); > private static long factorial(int num) < if (num 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.

Last updated: September 29, 2022

Источник

Install OpenJDK on Windows and Linux

    Ensure that you have enabled the Optional channel, by running the following commands:

yum-config-manager --enable rhel-7-server-optional-rpms
yum install java-11-openjdk-devel

* In order to configure Red Hat JBoss Developer Studio or Eclipse to use the OpenJDK 11, follow these instructions.

Red Hat Enterprise Linux 6 Installation

To install OpenJDK 11 on Red Hat Enterprise Linux 6:

  1. Ensure that you are subscribed to the base channel.
  2. Install the OpenJDK 11 package, by running the following command:
yum install java-11-openjdk-devel

Recent Articles

New Delve features in RHEL 9.2

How to deploy applications using Ansible Automation Platform

How to balance per-CPU upcall dispatch mode in Open vSwitch

What’s new in the Red Hat UBI OpenJDK containers

How to retrieve packet drop reasons in the Linux kernel

Comments

Products

Build

Communicate

RED HAT DEVELOPER

We serve the builders. The problem solvers who create careers with code.

Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

We’ve updated our Privacy Statement effective July 1, 2023

Источник

Оцените статью