Changing Java versions (Linux)

All on-domain Red Hat Enterprise Linux (RHEL) systems are running OpenJDK Java as the system default, however we have OracleJDK also available, this document aims to show users how to change their java version to meet their use-case.

Currently the Linux Lab image contains 6 versions of Java: 

  • OpenJDK 1.8 
  • OpenJDK 11
  • OpenJDK 17 
  • OracleJDK 8
  • OracleJDK 11
  • OracleJDK 17

Changing Java versions with Scripts

These scripts are designed to modify your users profile (located at '$HOME/.profile') so that any new bash spawning terminals or bash shells you start will make your default java run with the selected version as well as the shell you sourced the script from.

This change is not retroactive, so it will require any open shell session to be reloaded or resourced.  This change is persistent across all on-domain machines, logins, and reboots.

NOTE: Bash is the default shell, however if you have customized your environment things may not work as intended.

These scripts are located at the following location: `/opt/java-selection/`

In order to activate a java version simply run the script corresponding to the version you would like to work with and then reloading or resourcing your terminal:  For example the following command activates OpenJDK Java 8:
`/opt/java-selection/openjdk-java-8.bash`

These scripts change your Java version; there are scripts for OpenJDK Java and Oracle Java for each version installed. They set the JAVA_HOME profile variable to be retained in all future terminals and shells, but may require you to `source ~/.profile` in order to reload your environment after running the script.

  • /opt/java-selection/openjdk-java-<version>.bash
  • /opt/java-selection/oracle-java-<version>.bash

This script removes the changes made by the other scripts in this directory from your profile.  All new terminals and shells after this will default to the system Java which is based on the system alternatives.

/opt/java-selection/system-default-java.bash

This document is a reminder of what each script in the directory does.

/opt/java-selection/README.txt:

NOTE: In order for the changes to propagate beyond the shell that the script is sourced in the user must log out and log back into their session.

Changing Your Java Version Manually

If you prefer to do this manually and not use the scripts, you may simply add two lines to your shell profile, the two lines are the following.  With $jdk_path being a variable pointing to the location of the version of JDK you wish to run.

export JAVA_HOME="${jdk_path}"

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

Below is a table of the paths for the various Java installation directories. 

Java Version Java Path
Oracle Java <version> /usr/java/jdk<version>
OpenJDK Java <version> /usr/lib/jvm/java-<version>-openjdk-<full version #>.x86_64
Example Version: OpenJDK Java 17 Example Path: /usr/lib/jvm/java-11-openjdk/usr/lib/jvm/java-17-openjdk-17.0.7.0.7-3.el8.x86_64
0% helpful - 1 review

Details

Article ID: 86595
Created
Wed 9/11/19 9:29 AM
Modified
Mon 7/3/23 11:27 AM