Skip to content

Instantly share code, notes, and snippets.

@stephenc
Created November 7, 2014 13:50
Show Gist options
  • Save stephenc/6d8befb8cdd0edd09b7d to your computer and use it in GitHub Desktop.
Save stephenc/6d8befb8cdd0edd09b7d to your computer and use it in GitHub Desktop.
usejava ()
{
local sel=$1.jdk
if [ -x "/Library/Java/JavaVirtualMachines/jdk$sel/Contents/Home/bin/java" -a ! -x "/Library/Java/JavaVirtualMachines/$1/Contents/Home/bin/java" ]
then
sel=jdk$sel
fi
local base=/Library/Java/JavaVirtualMachines
if [ -x "/System/Library/Java/JavaVirtualMachines/$sel/Contents/Home/bin/java" ]
then
base=/System/Library/Java/JavaVirtualMachines
fi
if [ -z "$1" -o ! -x "$base/$sel/Contents/Home/bin/java" ]
then
local prefix="Syntax: usejava "
for i in /Library/Java/JavaVirtualMachines/* /System/Library/Java/JavaVirtualMachines/*
do
if [ -x "$i/Contents/Home/bin/java" ]
then
/bin/echo -n "$prefix$(basename $i | sed -e "s/^jdk//;s/\.jdk$//;")"
prefix=" | "
fi
done
/bin/echo ""
else
if [ -z "$JAVA_HOME" ]
then
export PATH=$base/$sel/Contents/Home/bin:$PATH
else
export PATH=$(echo $PATH|sed -e "s:$JAVA_HOME/bin:$base/$sel/Contents/Home/bin:g")
fi
export JAVA_HOME=$base/$sel/Contents/Home
echo -n -e "\033]0;$(java -version 2>&1 | sed -e "s/.*\"\(.*\)\".*/Java \1/;q")\007"
fi
}
complete -o "default" -o "nospace" -W "$(for i in /Library/Java/JavaVirtualMachines/* /System/Library/Java/JavaVirtualMachines/*; do if test -x "$i/Contents/Home/bin/java"; then echo "$(basename $i | sed -e "s/^jdk//;s/\.jdk$//;")"; fi; done)" usejava
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment