Chapter 2. Installing Java on RHEL 8

Java is an environment for developing and running a wide range of platform-agnostic applications, from mobile applications to desktop and web applications and enterprise systems. Java is a general-purpose programming language. Red Hat provides an open-source implementation of the Java Platform SE (Standard Edition) called OpenJDK. It is included in the RHEL repository.

Applications are developed using the JDK (Java Development Kit). Applications are run on a JVM (Java Virtual Machine), which is included in the JRE (Java Runtime Environment) and the JDK. There is also a headless version of Java which has the smallest footprint and does not include the libraries needed for a user interface. The headless version is packaged in the headless subpackage.

Note

If you are unsure whether you need the JRE or the JDK, it is recommended that you install the JDK.

Note

To get short-term supported Java versions, use Extra Packages for Enterprise Linux (EPEL). For information see EPEL. The name of package is java-latest-openjdk.

The following sections provide instructions for installing Java on RHEL.

2.1. Installing a JRE on RHEL using yum

You can install an OpenJDK Java Runtime Environment (JRE) using the system package manager, yum. This requires root privileges.

The JRE is part of the OpenJDK.

Procedure

  1. Run the yum command, specifying the package you want to install:

    • For JRE 8: java-1.8.0-openjdk

      $ sudo yum install java-1.8.0-openjdk

    • For JRE 11: java-11-openjdk

      $ sudo yum install java-11-openjdk

  2. Check that the installation works:

    $ java -version
    openjdk version "1.8.0_242"
    OpenJDK Runtime Environment (build 1.8.0_242-b08)
    OpenJDK 64-Bit Server VM (build 25.242-b08, mixed mode)

2.2. Installing a JRE on RHEL using an archive portable bundle

You can install an OpenJDK Java Runtime Environment (JRE) using the ZIP bundle. This is useful if the Java administrator does not have root privileges.

Note

It is recommended that you create a parent directory to contain your JREs and create a symbolic link to the latest JRE using a generic path. This eases upgrades to later versions.

Procedure

  1. Download the latest version of the JRE ZIP bundle for Linux.
  2. Extract the contents of the ZIP bundle to a directory of your choice:

    $ mkdir ~/jres
    $ cd ~/jres
    $ tar -xf java-1.8.0-openjdk-1.8.0.242.b08-1.static.jre.openjdkportable.x86_64.tar.xz
  3. Create a generic path by using symbolic links to your JRE for easier upgrades:

    $ ln -s ~/jres/java-1.8.0-openjdk-1.8.0.242.b08-1.static.jre.openjdkportable.x86_64 ~/jres/java-8

  4. Configure the JAVA_HOME environment variable:

    $ export JAVA_HOME=~/jres/java-8
  5. Verify that JAVA_HOME environment variable is set correctly:

    $ printenv | grep JAVA_HOME
    JAVA_HOME=~/jres/java-8
    Note

    When installed using this method, Java will only be available for the current user.

  6. Add the bin directory of the generic JRE path to the PATH environment variable:

    $ export PATH="$JAVA_HOME/bin:$PATH"
  7. Verify that java -version works without supplying the full path:

    $ java -version
    openjdk version "1.8.0_242"
    OpenJDK Runtime Environment (build 1.8.0_242-b08)
    OpenJDK 64-Bit Server VM (build 25.242-b08, mixed mode)
Note

You can ensure that JAVA_HOME environment variable persists for the current user by exporting the environment variable in ~/.bashrc.

2.3. Installing a JDK on RHEL 8 using yum

You can install an OpenJDK (Open Java Development Kit) using the system package manager, yum. This requires root privileges.

Procedure

  1. Run the yum command, specifying the package you want to install:

    • For JDK 8: java-1.8.0-openjdk-devel

      $ sudo yum install java-1.8.0-openjdk-devel

    • For JDK 11: java-11-openjdk-devel

      $ sudo yum install java-11-openjdk-devel

  2. Check that the installation works:

    $ javac -version
    javac 1.8.0_242

2.4. Installing a JDK on RHEL 8 using an archive portable bundle

You can install an OpenJDK (Open Java Development Kit) using the ZIP bundle. This is useful if the Java administrator does not have root privileges.

Note

It is recommended that you create a parent directory to contain your JDKs and create a symbolic link to the latest JDK using a generic path. This eases upgrades to later versions.

Procedure

  1. Download the latest version of the JDK ZIP bundle for Linux.
  2. Extract the contents of the ZIP bundle to a directory of your choice:

    $ mkdir ~/jdks
    $ cd ~/jdks
    $ tar -xf java-1.8.0-openjdk-1.8.0.242.b08-1.static.jdk.openjdkportable.x86_64.tar.xz
  3. Create a generic path by using symbolic links to your JRE for easier upgrades:

    $ ln -s ~/jdks/java-1.8.0-openjdk-1.8.0.242.b08-1.static.jdk.openjdkportable.x86_64 ~/jdks/java-8

  4. Configure the JAVA_HOME environment variable:

    $ export JAVA_HOME=~/jdks/java-8
  5. Verify that JAVA_HOME environment variable is set correctly:

    $ printenv | grep JAVA_HOME
    JAVA_HOME=~/jdks/java-8
    Note

    When installed using this method, Java will only be available for the current user.

  6. Add the bin directory of the generic JDK path to the PATH environment variable:

    $ export PATH="$JAVA_HOME/bin:$PATH"
  7. Verify that javac -version works without supplying the full path:

    $ javac -version
    javac "1.8.0_242"
Note

You can ensure that JAVA_HOME environment variable persists for the current user by exporting the environment variable in ~/.bashrc.

2.5. Installing multiple major versions of OpenJDK on RHEL using yum

You can install multiple versions of openJDK using the system package manager, yum. This requires root privileges.

Prerequisites

  • A Red Hat Subscription Manager (RHSM) account with an active subscription that provides access to a repository that provides the OpenJDK versions you want to install.

Procedure

  1. Run the yum command, specifying the package you want to install:

    • For JRE 8: java-1.8.0-openjdk

      $ sudo yum install java-1.8.0-openjdk

    • For JDK 11: java-11-openjdk

      $ sudo yum install java-11-openjdk

  2. Check that the installation works:

    $ java -version
    openjdk version "1.8.0_242"
    OpenJDK Runtime Environment (build 1.8.0_242-b08)
    OpenJDK 64-Bit Server VM (build 25.242-b08, mixed mode)

You can configure the default Java version to use by using java --alternatives. For more information see Selecting a system-wide java version.

2.6. Installing multiple major versions of OpenJDK on RHEL using an archive portable bundle

You can install multiple major versions of OpenJDK by using the same procedures found in Installing a JRE on RHEL using an archive portable bundle or Installing a JDK on RHEL 8 using an archive portable bundle using multiple major versions.

Note

For instructions how to configure the default Java version for the system, see Selecting a system-wide zip-bundle java version.

2.7. Installing multiple minor versions of OpenJDK on RHEL using yum

You can install multiple minor versions of OpenJDK on RHEL. This is done by preventing the installed minor versions from being updated.

Procedure

  • Add the installonlypkgs option in /etc/yum.conf to specify the Java packages that yum can install but not update.

    installonlypkgs=java-<version>--openjdk,java-<version>--openjdk-headless,java-<version>--openjdk-devel

    Updates will install new packages while leaving the old versions on the system:

    $ rpm -qa | grep java-1.8.0-openjdk
    java-1.8.0-openjdk-1.8.0.242.b08-0.el8_1.x86_64
    java-1.8.0-openjdk-headless-1.8.0.242.b08-0.el8_1.x86_64
  • The different minor versions of OpenJDK can be found in the /usr/lib/jvm/<minor version> files.

    For example, the following shows part of /usr/lib/jvm/java-1.8.0-openjdk-1.8.0:

    $ /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.242.b08-0.el8_1.x86_64/bin/java -version
    openjdk version "1.8.0_242"
    OpenJDK Runtime Environment (build 1.8.0_242-b08)
    OpenJDK 64-Bit Server VM (build 25.242-b08, mixed mode)
    
    $ /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.172-7.b11.el7.x86_64/bin/java -version
    openjdk version "1.8.0_172"
    OpenJDK Runtime Environment (build 1.8.0_172-b11)
    OpenJDK 64-Bit Server VM (build 25.172-b11, mixed mode)

You can choose system-wide using version by following Selecting a system-wide java version.

2.8. Installing multiple minor versions of OpenJDK on RHEL using an archive portable bundle

Installing multiple minor versions is the same as Installing a JRE on RHEL using an archive portable bundle or Installing a JDK on RHEL 8 using an archive portable bundle using multiple minor versions.

Note

For instructions how to choose a default minor version for the system, see Selecting a system-wide zip-bundle java version.