Configuring OpenJDK 8 for RHEL
Abstract
Making open source more inclusive
Red Hat is committed to replacing problematic language in our code, documentation, and web properties. We are beginning with these four terms: master, slave, blacklist, and whitelist. Because of the enormity of this endeavor, these changes will be implemented gradually over several upcoming releases. For more details, see our CTO Chris Wright’s message.
Providing feedback on Red Hat documentation
We appreciate your feedback on our documentation. To provide feedback, you can highlight the text in a document and add comments.
This section explains how to submit feedback.
Prerequisites
- You are logged in to the Red Hat Customer Portal.
- In the Red Hat Customer Portal, view the document in Multi-page HTML format.
Procedure
To provide your feedback, perform the following steps:
Click the Feedback button in the top-right corner of the document to see existing feedback.
NoteThe feedback feature is enabled only in the Multi-page HTML format.
- Highlight the section of the document where you want to provide feedback.
Click the Add Feedback pop-up that appears near the highlighted text.
A text box appears in the feedback section on the right side of the page.
Enter your feedback in the text box and click Submit.
A documentation issue is created.
- To view the issue, click the issue tracker link in the feedback view.
Chapter 1. Interactively selecting a system-wide OpenJDK version on RHEL
If you have multiple versions of OpenJDK installed on RHEL, you can interactively select the default OpenJDK version to use system-wide.
If you do not have root privileges, you can select an OpenJDK version by configuring the JAVA_HOME
environment variable.
Prerequisites
- You must have root privileges on the system.
-
Multiple versions of OpenJDK were installed using the
yum
package manager.
Procedure
View the OpenJDK 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
Display the OpenJDK 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 OpenJDK version is marked with an asterisk.
-
The current OpenJDK version for the specified
java
command is marked with a plus sign.
Press Enter to keep the current selection or enter the Selection number of the OpenJDK version you want to select followed by the Enter key.
The default OpenJDK version for the system is the selected version.
Verify that the chosen 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)
NoteThis procedure configures the
java
command. Thenjavac
command can be set up in a similar way, but it operates independently.If you have OpenJDK installed,
alternatives
provides more possible selections. In particular, thejavac
master alternative switches many binaries provided by the-devel
sub-package.Even if you have OpenJDK installed,
java
(and other JRE masters) andjavac
(and other OpenJDK masters) still operate separately, so you can have different selections for JRE and JDK. Thealternatives --config java
command affects thejre
and its associated slaves.If you want to change the
Java
, use thejavac alternatives
command. The--config javac
utility configures theSDK
and related slaves. To see all possible masters, usealternatives --list
and check all of thejava
,javac
,jre
, andsdk
masters.
Chapter 2. Non-interactively selecting a system-wide OpenJDK version on RHEL
If you have multiple versions of OpenJDK installed on RHEL, you can select the default OpenJDK 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 OpenJDK on many systems in an automated way.
If you do not have root privileges, you can select an OpenJDK version by configuring the JAVA_HOME
environment variable.
Prerequisites
- You must have root privileges on the system.
-
Multiple versions of OpenJDK were installed using the
yum
package manager.
Procedure
Select the major OpenJDK version to switch to. For example, for OpenJDK 8, use java-1.8.0-openjdk.
# PKG_NAME=java-1.8.0-openjdk # JAVA_TO_SELECT=$(alternatives --display java | grep "family $PKG_NAME" | cut -d' ' -f1) # alternatives --set java $JAVA_TO_SELECT
Verify that the active OpenJDK version is the one you specified.
$ 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)
NoteA similar approach can be followed for
javac
.
Chapter 3. Selecting an installed OpenJDK version for a specific application
Some applications require a specific OpenJDK version to run. If multiple versions of OpenJDK are installed on the system using the yum
package manager or portable bundle, you can select a OpenJDK 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 OpenJDK installed on the machine.
- Ensure that the application you want to run is installed.
Procedure
Set the
JAVA_HOME
environment variable. For example, if openjdk-8 was installed usingyum
:$ JAVA_HOME=/usr/lib/jvm/java-8-openjdk
NoteThe symbolic link
java-8-openjdk
is controlled by thealternatives
command.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-8-openjdk 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-8-openjdk-1.8.0.242.b08-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"
Chapter 4. Selecting a system-wide archive OpenJDK version
If you have multiple versions of OpenJDK installed with the archive on RHEL 8, you can select a specific OpenJDK version to use system-wide.
Prerequisites
- Know the locations of the OpenJDK versions installed using the archive.
Procedure
To specify the OpenJDK version to use for a single session:
Configure
JAVA_HOME
with the path to the OpenJDK version you want used system-wide.$ export JAVA_HOME=/opt/jdk/jdk-1.8.0
Add
$JAVA_HOME/bin
to thePATH
environment variable.$ export PATH="$JAVA_HOME/bin:$PATH"
To specify the OpenJDK version to use permanently for a single user, add these commands into ~/.bashrc
:
export JAVA_HOME=/opt/jdk/jdk-1.8.0 export PATH="$JAVA_HOME/bin:$PATH"
To specify the OpenJDK version to use permanently for all users, add these commands into /etc/bashrc
:
export JAVA_HOME=/opt/jdk/jdk-1.8.0 export PATH="$JAVA_HOME/bin:$PATH"
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-1.8.0/bin:$PATH"
Additional resources
-
For more information about the exact meaning of
JAVA_HOME
, see Changes/Decouple system java setting from java command setting.
Chapter 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 OpenJDK installation.
Prerequisites
-
You know where you installed OpenJDK on your system. For example,
/opt/jdk/11
.
Procedure
Set the value of
JAVA_HOME
.$ export JAVA_HOME=/opt/jdk/11
Verify that
JAVA_HOME
is set correctly.$ printenv | grep JAVA_HOME JAVA_HOME=/opt/jdk/11
NoteYou 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. Persistent means that if you close your terminal or reboot your computer, you do not need to reset a value for theJAVA_HOME
environment variable.The following example demonstrates using a text editor to enter commands for exporting
JAVA_HOME
in~/.bashrc
for a single user:> vi ~/.bash_profile export JAVA_HOME=/opt/jdk/11 export PATH="$JAVA_HOME/bin:$PATH"
Additional resources
-
Be aware of the exact meaning of
JAVA_HOME
. For more information, see Changes/Decouple system java setting from java command setting.
Chapter 6. Configuring the heap size for OpenJDK application on RHEL
You can configure OpenJDK 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:$ java -Xmx100m <your_application_name>
Additional resources
-
For more information about the
Xmx
option, see -Xmxsize in the Java documentation.