Changing Java versions in Linux

Currently, all on-domain Red Hat Enterprise Linux systems are running OpenJDK Java 11 as the system default, some users still need different versions of Java for their use cases, this document aims to allow users to change their own java version for their own use cases.

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.

These scripts are located at the following location:

`/opt/java-selection/`

In order to activate these scripts simply run the command `source $SCRIPT`, where “$SCRIPT” is the location of the script.  For example, the following command activates OpenJDK Java 8:

`source /opt/java-selection/openjdk-java-8.bash`

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.  With $jdk-install-dir being a variable, replaceable 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/jdk1.8.0_181-amd64
OpenJDK Java 8 /usr/lib/jvm/java-1.8.0-openjdk
OpenJDK Java 11 /usr/lib/jvm/java-11-openjdk
Was this helpful?
100% helpful - 1 review

Details

Article ID: 86595
Created
Wed 9/11/19 9:29 AM
Modified
Wed 9/11/19 9:57 AM