Changing Java versions (Linux)

Summary

How to change Java versions in Linux

Body

All on-domain Red Hat Enterprise Linux (RHEL) systems are running OpenJDK Java as the system default. However, OracleJDK is also available. This article demonstrates how users can change their Java version to meet their use case.

Currently, the Linux Lab image contains 8 versions of Java: 

  • OpenJDK 1.8.0
  • OpenJDK 11
  • OpenJDK 17
  • OpenJDK 21
  • OracleJDK 8
  • OracleJDK 11
  • OracleJDK 17
  • Oracle JDK 21

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, run the script corresponding to the version you would like to work with, then reload or resource your terminal:  For example the following command activates OpenJDK Java 8:
`source /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.

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

  • source /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.

source /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

Details

Details

Article ID: 86595
Created
Wed 9/11/19 9:29 AM
Modified
Mon 10/14/24 3:09 PM