Skip to content

Instantly share code, notes, and snippets.

@chenasraf
Last active August 25, 2022 12:24
Show Gist options
  • Save chenasraf/bdab51f3916f7c06b15fc3610a0cc207 to your computer and use it in GitHub Desktop.
Save chenasraf/bdab51f3916f7c06b15fc3610a0cc207 to your computer and use it in GitHub Desktop.
Java SDK version switcher

jver version switcher

A simple script to allow to switch easily between JDK versions on a single machine, in case several versions are needed.

Usage:

jver list # list all available versions in $BASE_DIR
jver 17 # switch to version 17
jver 17 -q # suppress output
jver # output current version

Version is persisted on the user home directory.

To use, put the file somewhere, and load it with source /path/to/jver.sh in your .bashrc file (or other file depending on your terminal environment). jver is a function in that file.

#!/usr/bin/env bash
# helper fns
color() {
echo -e "\033[0;$1m$2\033[0m"
}
echo_red() {
color 31 "$1"
}
echo_yellow() {
color 33 "$1"
}
echo_cyan() {
color 36 "$1"
}
# update these if necessary for your machine
BASE_DIR="/Library/Java/JavaVirtualMachines"
DIR_SUFFIX="Contents/Home"
java() {
$JAVA_HOME/bin/java $@
}
jver_file="$HOME/.jver"
__list_jvers__() {
ls $BASE_DIR
}
# main script fn
jver() {
ver="$1"
quiet="$2"
verbose=$([[ $quiet == '-q' ]] && echo 0 || echo 1)
if [[ "$ver" == "list" ]]; then
echo
echo_cyan "All installed versions on this machine:"
echo
__list_jvers__ | tr " " "\n"
echo
echo_yellow 'You can use "jver [version]" to switch to the required version.'
echo_yellow 'Supplying the [version] is done via grep, so can support partial matches or patterns.'
echo_yellow 'For example, "jver 17" will match correctly for "jdk-17.0.2.jdk"'
return 0
fi
if [[ $ver == "" ]]; then
echo_red "No version supplied. Usage: jver [version]"
echo_yellow "Current version: $(cat $jver_file)"
return 1
fi
count=$(__list_jvers__ | grep -i $ver -c)
found=$(__list_jvers__ | grep -i $ver)
if [[ $count -gt 1 ]]; then
echo_red "Multiple versions found:"
echo
echo_red $found | tr " " "\n"
echo
echo_red "Please use a more specific pattern so that only one result matches"
return 2
fi
if [[ "$found" == "" ]]; then
echo_red "Version $ver not found"
echo_yellow "Possible versions are:"
__list_jvers__ | tr " " "\n"
return 2
fi
export JAVA_HOME="$BASE_DIR/$found/$DIR_SUFFIX"
touch $jver_file
echo $found >$jver_file
if [[ $verbose -eq 1 ]]; then
echo_cyan "Found version: $found"
echo
java -version
fi
}
if [[ -f $jver_file ]]; then
jver $(cat $jver_file) -q
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment