How to set the JAVA_HOME environment variable?
Environment
- Red Hat Enterprise Linux
- Red Hat build of OpenJDK (ZIP installation)
Issue
- Requirement to set the
JAVA_HOMEenvironment variable to run JBoss products. - Need instructions on permanently setting the
JAVA_HOMEenvironment variable. - The Java home directory is missing or changes after rebooting Red Hat Enterprise Linux.
Resolution
Open the appropriate shell configuration file using a text editor:
- For system-wide configuration affecting all users, edit
/etc/bashrc. - For a single user configuration, edit
~/.bashrc.
Add the following lines to the end of the file. Ensure to replace <path/to/java/home> with the absolute path where the OpenJDK ZIP was extracted:
export JAVA_HOME=<path/to/java/home>
export PATH="$JAVA_HOME/bin:$PATH"
Apply the changes to the current terminal session by sourcing the edited configuration file:
source ~/.bashrc
Note: This solution is for the ZIP installation of OpenJDK. The RPM-based installation uses the
alternativescommand to manage Java environments, which is described in Switching JAVA_HOME using alternatives.
Root Cause
Variables set using the export command directly in the command line interface are only valid for the duration of the current shell session. When the system is rebooted or the terminal session is closed, the JAVA_HOME variable is lost unless it is persistently defined in a shell initialization script (such as /etc/bashrc or ~/.bashrc).
Diagnostic Steps
-
Check the current value of the
JAVA_HOMEenvironment variable to confirm it is set correctly:echo $JAVA_HOME -
Verify the active Java version and the executable path being prioritized by the system:
java -version which java
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.
Comments