Changing Java versions in Linux

Currently, all on-domain Red Hat Enterprise Linux systems are running OpenJDK Java 11 as the system default. You can change your own Java version, if you need a different version.

Currently the Linux Lab image contains 3 versions of Java: 

  • Oracle’s Java 8 [v1.8]
  • OpenJDK Java 8 [v1.8]
  • OpenJDK Java 11 [v11.04] (default).  

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.

NOTE: Bash is the default shell if you have not manually changed this 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 on-domain 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.

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.

Java version

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"

Java 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 reviews

Details

Article ID: 86595
Created
Wed 9/11/19 9:29 AM
Modified
Thu 10/1/20 9:31 AM