Skip to content

Instantly share code, notes, and snippets.

@alexejk
Last active August 29, 2015 13:56
Show Gist options
  • Save alexejk/9039577 to your computer and use it in GitHub Desktop.
Save alexejk/9039577 to your computer and use it in GitHub Desktop.
#List installed Java versions
function jls() {
/usr/libexec/java_home -V 2>&1 | grep -E "\d.\d.\d" | cut -d , -f 1 | colrm 1 4 | grep -v Home
}
#Change active java version
function jset() {
local isLink=false
if ! [ "x$JAVA_HOME" = "x" ]; then
if [ -h $JAVA_HOME ]; then
isLink=true
sudo unlink $JAVA_HOME
else
if ! [ -e "$JAVA_HOME" ]; then
isLink=true
fi
fi
else
echo "JAVA_HOME env variable not set. It will be exported for this session but will be lost after restart"
fi
local newHome=$(/usr/libexec/java_home -v $1)
if $isLink; then
sudo ln -s $newHome $JAVA_HOME
else
export JAVA_HOME=$newHome
fi
java -version
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment