Switching Java Versions (Update)
This is an update to the previous article Switching Java versions.
Here is an improved version of the script, to switch between different java versions. You
can put it into your .bashrc
.
function switch_java_home {
echo "-----------------------------"
echo "Switching Java"
if [ ! -z "${JAVA_HOME}" ]; then
echo "Old: $JAVA_HOME";
PATH="${PATH/$JAVA_HOME\/bin:/}"
fi
JAVA_HOME=$1
echo "JAVA_HOME: ${JAVA_HOME}"
PATH="${JAVA_HOME}/bin:$PATH"
export JAVA_HOME
export PATH
java -version
echo "-----------------------------"
}
alias oraclejava6="switch_java_home $HOME/programs/oraclejdk/jdk1.6.0_45"
alias oraclejava7="switch_java_home $HOME/programs/oraclejdk/jdk1.7.0_80"
alias oraclejava8="switch_java_home $HOME/programs/oraclejdk/jdk1.8.0_231"
alias oraclejava9="switch_java_home $HOME/programs/oraclejdk/jdk-9.0.4"
alias oraclejava10="switch_java_home $HOME/programs/oraclejdk/jdk-10.0.2"
alias oraclejava11="switch_java_home $HOME/programs/oraclejdk/jdk-11.0.5"
alias openjdk8="switch_java_home /usr/lib/jvm/java-8-openjdk-amd64"
alias openjdk9="switch_java_home $HOME/programs/openjdk/jdk-9.0.4"
alias openjdk10="switch_java_home $HOME/programs/openjdk/jdk-10.0.2"
alias openjdk11="switch_java_home $HOME/programs/openjdk/jdk-11.0.5+10"
alias openjdk12="switch_java_home $HOME/programs/openjdk/jdk-12.0.2"
alias openjdk13="switch_java_home $HOME/programs/openjdk/jdk-13.0.1+9"
oraclejava13
The function switch_java_home
does several things: Removing the previous java path from your PATH
environment
variable, so that it stays clean; adding the new java path; logging out the new java version in the console.
The aliases provide nice shortcuts to your available java installations.
The last command oraclejava13
makes that to your default java version after you opened a shell.
Example for switching between oraclejava11 and openjdk11:
$ oraclejava11
-----------------------------
Switching Java
JAVA_HOME: /home/andreas/programs/oraclejdk/jdk-11.0.5
java version "11.0.5" 2019-10-15 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.5+10-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.5+10-LTS, mixed mode)
-----------------------------
$ openjdk11
-----------------------------
Switching Java
Old: /home/andreas/programs/oraclejdk/jdk-11.0.5
JAVA_HOME: /home/andreas/programs/openjdk/jdk-11.0.5+10
openjdk version "11.0.5" 2019-10-15
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.5+10)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.5+10, mixed mode)
-----------------------------
With that in place, it’s easy to switch between various java versions.
By the way: As you see, I use the open jdk variant from AdoptOpenJdk.
Comments
No comments yet.Leave a comment
Your email address will not be published. Required fields are marked *. All comments are held for moderation to avoid spam and abuse.