Chapter 3. Configuring Java on RHEL 8

The following sections provide instructions for configuring Java on RHEL.

3.1. Interactively selecting a system-wide Java version on RHEL

If you have multiple versions of Java installed on RHEL, you can interactively select the default Java version to use system-wide. This requires root privileges.

Note

If you do not have root privileges, you can select a Java version by configuring the JAVA_HOME environment variable.

Prerequisites

  • You must have root privileges on the system.
  • Multiple versions of Java were installed using the yum package manager.

Procedure

  1. View the Java versions installed on the system.

    $ yum list installed "java*"

    A list of installed Java packages appears.

    Installed Packages
    java-1.8.0-openjdk.x86_64                     1:1.8.0.242.b08-0.el8_1                     @rhel-8-appstream-rpms
    java-1.8.0-openjdk-headless.x86_64            1:1.8.0.242.b08-0.el8_1                     @rhel-8-appstream-rpms
    java-11-openjdk.x86_64                        1:11.0.6.10-0.el8_1                         @rhel-8-appstream-rpms
    java-11-openjdk-headless.x86_64               1:11.0.6.10-0.el8_1                         @rhel-8-appstream-rpms
    javapackages-filesystem.noarch                5.3.0-1.module+el8+2447+6f56d9a6            @rhel-8-appstream-rpms
  2. Display the Java versions that can be used for a specific Java command and select the one to use:

    $ 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.242.b08-0.el8_1.x86_64/jre/bin/java)
       2           java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.6.10-0.el8_1.x86_64/bin/java)
    
    
    Enter to keep the current selection[+], or type selection number: 1
    • The current system-wide Java version is marked with an asterisk.
    • The current Java version for the specified Java command is marked with a plus sign.
  3. Press Enter to keep the current selection or enter the Selection number of the Java version you want to select followed by the Enter key.

    The default Java version for the system is the selected version.

  4. Verify that the selected binary is selected.

    $ 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

    This procedure configures the java command. Then javac command can be set up in a similar way, but it operates independently.

    If you have a JDK installed, alternatives provides more possible selections. In particular, the javac master alternative switches many binaries provided by the -devel sub-package. Even if you have JDK installed, java (and other JRE masters) and javac (and other JDK masters) still operate separately, so you can have different selections for JRE and JDK.

    The alternatives --config java command affects the jre and its associated slaves. If you want to change the JDK, use the javac alternatives command. The --config javac utility configures the SDK and related slaves. To see all possible masters, use alternatives --list and check all of the java,javac, jre, and sdk masters.

3.2. Non-interactively selecting a system-wide Java version on RHEL

If you have multiple versions of Java installed on RHEL, you can select the default Java version to use system-wide in a non-interactive way. This is useful for administrators who have root privileges on a Red Hat Enterprise Linux system and need to switch the default Java on many systems in an automated way.

Note

If you do not have root privileges, you can select a Java version by configuring the JAVA_HOME environment variable.

Prerequisites

  • You must have root privileges on the system.
  • Multiple versions of Java were installed using the yum package manager.

Procedure

  1. Select the major Java version to switch to. For example, for Java 11, use java-11-openjdk.

    # PKG_NAME=java-11-openjdk`
    # JAVA_TO_SELECT=$(alternatives --display java | grep "family $PKG_NAME" | cut -d' ' -f1)`
    # alternatives --set java $JAVA_TO_SELECT`
  2. Verify that the active Java version is the one you specified.

    $ java -version
    openjdk version "11.0.3" 2019-04-16 LTS
    OpenJDK Runtime Environment 18.9 (build 11.0.3+7-LTS)
    OpenJDK 64-Bit Server VM 18.9 (build 11.0.3+7-LTS, mixed mode, sharing)
    Note

    For Java 8, set PKG_NAME to java-1.8.0-openjdk.

    Note

    A similar approach can be followed for javac.

3.3. Selecting an installed Java version for a specific application

Some applications require a specific Java version to run. If multiple versions of Java are installed on the system using the yum package manager or portable bundle, you can select a Java version for each application where necessary by setting the value of the JAVA_HOME environment variable or using a wrapper script.

Prerequisites

  • Multiple versions of Java installed on the machine.
  • Ensure that the application you want to run is installed.

Procedure

  1. Set the JAVA_HOME environment variable. For example, if openjdk-11 was installed using yum:

    $ JAVA_HOME=/usr/lib/jvm/java-11-openjdk

    Note

    The symbolic link java-11-openjdk is controlled by the alternatives command.

  2. Do one of the following:

    • Launch the application using the default, system-wide configuration.

      $ mvn --version
      Apache Maven 3.5.4 (Red Hat 3.5.4-5)
      Maven home: /usr/share/maven
      Java version: 1.8.0_242, vendor: Oracle Corporation, runtime: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.242.b08-0.el8_1.x86_64/jre
      Default locale: en_US, platform encoding: UTF-8
      OS name: "linux", version: "4.18.0-147.3.1.el8_1.x86_64", arch: "amd64", family: "unix"
    • Launch the application specifying the JAVA_HOME variable:

      $ 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.5, vendor: Oracle Corporation, runtime: /usr/lib/jvm/java-11-openjdk-11.0.5.10-0.el8_1.x86_64
      Default locale: en_US, platform encoding: UTF-8
      OS name: "linux", version: "5.4.12-200.el8_1.x86_64", arch: "amd64", family: "unix"

3.4. Selecting a system-wide ZIP-bundle Java version

If you have multiple version of Java installed on RHEL 8 using ZIP bundles, you can select a specific Java version to use system-wide.

Prerequisites

  • Know the locations of the Java versions installed using ZIP bundles.

Procedure

To specify the Java version to use for a single session:

  1. Configure JAVA_HOME with the path to the Java version you want used system-wide.

    $ export JAVA_HOME=/opt/jdk/jdk-11.0.3

  2. Add $JAVA_HOME/bin to the PATH environment variable.

    $ export PATH="$JAVA_HOME/bin:$PATH"

To specify the Java version to use permanently for a single user, add these commands into ~/.bashrc:

export JAVA_HOME=/opt/jdk/jdk-11.0.3
export PATH="$JAVA_HOME/bin:$PATH"

To specify the Java version to use permanently for all users, add these commands into /etc/bashrc:

export JAVA_HOME=/opt/jdk/jdk-11.0.3
export PATH="$JAVA_HOME/bin:$PATH"
Note

Be aware of the exact meaning of JAVA_HOME. For more information, see Changes/Decouple system java setting from java command setting. If you do not want to redefine JAVA_HOME, add only the PATH command to bashrc, specifying the path to the Java binary. For example, export PATH="/opt/jdk/jdk-11.0.3/bin:$PATH".

3.5. Configuring the JAVA_HOME environment variable on RHEL

Some applications require you to set the JAVA_HOME environment variable so that they can find the Java installation.

Prerequisites

  • Know where Java is installed on your system. For example, /opt/jdk/11.

Procedure

  1. Set the value of JAVA_HOME.

    $ export JAVA_HOME=/opt/jdk/11

  2. Verify that JAVA_HOME is set correctly.

    $ printenv | grep JAVA_HOME
    JAVA_HOME=/opt/jdk/11
    Note

    You can make the value of JAVA_HOME persistent by exporting the environment variable in ~/.bashrc for single users or /etc/bashrc for system wide settings.

    Note

    Be aware of the exact meaning of JAVA_HOME. For more information, see Changes/Decouple system java setting from java command setting.

3.6. Configuring the heap size for a Java application on RHEL

OpenJDK can be configured to use a customized heap size.

Procedure

  • Add the maximum heap size option to the Java command when running your application

    For example to set the maximum heap size to 100 megabytes use the -Xmx100m option.

    For OpenJDK 8:

    $ java -Xmx100m <your-application>

    For OpenJDK 11 it is recommended to use a new way:

    $ java -Xmn100m <your-application>

    For more information about the Xmx option, see -Xmxsize in Java documentation.