Changing Java versions (Linux)

All fully managed Red Hat Enterprise Linux systems run a certain Java version as the default. You can change your own Java version to what you need, as the Linux Lab image contains multiple versions of Java.

Changing Java Versions Automatically

These scripts are designed to modify your local bashrc profile (located at '$HOME/.bashrc') 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. Bash is the default shell. If you have not manually changed this, then you are using bash, and these scripts will work as intended. This change is not retroactive, so it will not work on shells and terminals already open (except for the one you sourced the script from).  This change is persistent across all fully managed machines, logins, and reboots.

Activate Scripts

You can find the scripts at '/opt/java-selection/'.

In order to activate these scripts, simply run the command 'source $SCRIPT', where "$SCRIPT" is the location of the script, and then log out and back in.  For example, the following command activates OpenJDK Java 8: source /opt/java-selection/openjdk-java-8.bash. 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.

For example:

[blizzard@guardian ~]$ java -version
openjdk version "11.0.11" 2021-04-20 LTS
OpenJDK Runtime Environment 18.9 (build 11.0.11+9-LTS)

OpenJDK 64-Bit Server VM 18.9 (build 11.0.11+9-LTS, mixed mode, sharing)
[blizzard@guardian ~]$ source /opt/java-selection/oracle-java-8.bash
JAVA_HOME not previously set, setting...
Java now set to Oracle Java 8

[blizzard@guardian ~]$ java -version
java version "1.8.0_181"
Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)

The script locations are listed below, with details on what each does.

/opt/java-selection/openjdk-java-8.bash:

  • This script changes your Java version to OpenJDK Java 8 in your current terminal/shell, and all future terminals and shells.

/opt/java-selection/oracle-java-8.bash:

  • This script changes your Java version to Oracle Java 8 in your current terminal/shell, and all future terminals and shells.

/opt/java-selection/openjdk-java-11.bash:

  • This script changes your Java version to OpenJDK Java 11 in your current terminal/shell, and all future terminals and shells.

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

  •  This script removes the changes made by the other scripts in this directory from your bashrc file.  All new terminals and shells after this will default to the system Java (currently OpenJDK 11).

/opt/java-selection/README.txt:

  •  This file is not a script, but rather a small text document as a quick reminder of what each script in the directory does.

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, You can replace $jdk-install-dir  by one of the paths below.
export JAVA_HOME="$jdk-install-dir"
export PATH="${JAVA_HOME}/bin:{$PATH}"

For example:
export JAVA_HOME="/usr/lib/jvm/java-1.8.0-openjdk"
export PATH="${JAVA_HOME}/bin:{$PATH}"

Below is a table of the paths for the various Java installation directories, these may be substituted with the "jdk-install-dir" variable in Oracle’s documentation.

Java Install Directories
Java Version jdk-install-dir
Oracle Java 8 /usr/java/latest
OpenJDK Java 8 /usr/lib/jvm/java-1.8.0-openjdk
OpenJDK Java 11 /usr/lib/jvm/java-11-openjdk
Was this helpful?
0% helpful - 1 review

Details

Article ID: 86595
Created
Wed 9/11/19 9:29 AM
Modified
Tue 3/15/22 8:34 AM