Last active
May 26, 2021 14:59
-
-
Save nisrulz/e90bddac3dbd2f6b8d4adc7b150c07e9 to your computer and use it in GitHub Desktop.
Bash function to switch jdk version in the terminal session only.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Function to switch JDK versions | |
# Usage: | |
# > setjdk # switch to latest jdk | |
# > setjdk 8 # switch to jdk 1.8 | |
# > setjdk 11 # switch to jdk 11 | |
function setjdk() { | |
oldStyleVersion=8 | |
requestedVersion=$1 | |
finalVersion="" | |
# Figure out the version | |
if [ $# -eq 0 ]; then | |
echo "Setting to latest Java version" | |
elif [ $requestedVersion -gt $oldStyleVersion ]; then | |
echo "Switching to Java version: $1"; | |
finalVersion="-v $1"; | |
else | |
echo "Switching to Java version: $1"; | |
finalVersion="-v 1.$1"; | |
fi | |
# Set the version | |
export JAVA_HOME=$(/usr/libexec/java_home $finalVersion); | |
java -version; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Looks like this: