Skip to content

Instantly share code, notes, and snippets.

@wayneeseguin
Created August 3, 2011 14:34
Show Gist options
  • Save wayneeseguin/1122780 to your computer and use it in GitHub Desktop.
Save wayneeseguin/1122780 to your computer and use it in GitHub Desktop.
pickjdk shell function refactored from Nick Sieger's script
#!/bin/bash
#
# Quicky Waynification of Nick's pickjdk
#
#
# pickjdk: for switching between Java versions:
# From Nick Sieger: http://pastie.org/170326
# "I just symlink soylatte to:
# /System/Library/Frameworks/JavaVM.framework/Versions/1.6.0-soylatte/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0-soylatte"
#
pickjdk()
{
: \
"${SOYLATTE_HOME:="${HOME}/dev/java/src/soylatte/control/build/bsd-amd64"}" \
"${SOYLATTE_32_HOME:="/usr/local/soylatte16-i386-1.0.3"}" \
"${JAVA_1_7_0_HOME:="/usr/local/java-1.7.0"}"
if [[ -z "${JDKS_ROOT:-}" ]]
then
if [[ -d "/System/Library/Frameworks/JavaVM.framework/Versions" ]]
then
JDKS_ROOT="/System/Library/Frameworks/JavaVM.framework/Versions"
else
echo "JDKS_ROOT must be set in order to use pickjdk."
return 1
fi
fi
local n=1 jdk total_jdks choice=0 current="$JAVA_HOME" explicit_jdk="" jdks=()
local jdk_dirs=($(
find "${JDKS_ROOT}" -iname '[0-9]*' -mindepth 1 -maxdepth 1 -type d
))
for jdk in "${jdk_dirs[@]}"
do
echo -n " $n) ${jdk} ${jdk##*/}"
if [[ "$OSTYPE" =~ *darwin* ]]
then
jdk="$jdk/Home"
fi
if [[ $jdk == "$current" ]]
then
echo " < CURRENT"
else
echo
fi
jdks[$n]="$jdk"
(( n++ ))
total_jdks=$n
done
echo " $n) Soylatte-amd64"
jdks[$n]="$SOYLATTE_HOME"
(( n++ ))
echo " $n) Soylatte16-i386-1.0.3"
jdks[$n]="$SOYLATTE_32_HOME"
(( n++ ))
echo " $n) 1.7.0"
jdks[$n]="$JAVA_1_7_0_HOME"
(( n++ ))
echo " $n) None"
jdks[$n]=None
total_jdks=$n
if (( total_jdks > 1 ))
then
while [[ -z "${jdks[$choice]}" ]]
do
echo -n "Choose one of the above [1-$total_jdks]: "
read choice
done
else
choice=1
fi
if [[ -z "$current" ]]
then
current=$(dirname $(dirname $(type -path java)))
fi
if [[ "${jdks[$choice]}" != None ]]
then
export JAVA_HOME="${jdks[$choice]}"
else
unset JAVA_HOME
fi
for jdk in "${jdks[@]}"
do
if [[ "$current" == "$jdk" ]]
then
explicit_jdk="$jdk"
break # Found it!!! w00t!
fi
done
if [[ "$explicit_jdk" ]]
then
if [[ -z "$JAVA_HOME" ]]
then
PATH="$(printf "$PATH" | sed "s|$explicit_jdk/bin:*||g")"
else
PATH="$(printf "$PATH" | sed "s|$explicit_jdk|$JAVA_HOME|g")"
fi
elif [[ -n "$JAVA_HOME" ]]
then
PATH="$JAVA_HOME/bin:$PATH"
fi
hash -r
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment