Skip to content

Instantly share code, notes, and snippets.

@nicksieger
Forked from wayneeseguin/gist:1122780
Created August 3, 2011 17:58
Show Gist options
  • Save nicksieger/1123322 to your computer and use it in GitHub Desktop.
Save nicksieger/1123322 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
}
$ . pickwayne.sh
[13:56:33][~]
$ pickjdk
1) /Library/Java/JavaVirtualMachines/1.5.0.jdk 1.5.0.jdk
2) /Library/Java/JavaVirtualMachines/1.6.0-soylatte.jdk 1.6.0-soylatte.jdk
3) /Library/Java/JavaVirtualMachines/1.6.0_22-b04-307.jdk 1.6.0_22-b04-307.jdk
4) /Library/Java/JavaVirtualMachines/1.7.0.jdk 1.7.0.jdk
5) Soylatte-amd64
6) Soylatte16-i386-1.0.3
7) 1.7.0
8) None
Choose one of the above [1-8]: 1
[13:56:47][~]
$ pickjdk
1) /Library/Java/JavaVirtualMachines/1.5.0.jdk 1.5.0.jdk < CURRENT
2) /Library/Java/JavaVirtualMachines/1.6.0-soylatte.jdk 1.6.0-soylatte.jdk
3) /Library/Java/JavaVirtualMachines/1.6.0_22-b04-307.jdk 1.6.0_22-b04-307.jdk
4) /Library/Java/JavaVirtualMachines/1.7.0.jdk 1.7.0.jdk
5) Soylatte-amd64
6) Soylatte16-i386-1.0.3
7) 1.7.0
8) None
Choose one of the above [1-8]: ^C
[exited with 1]
[13:56:54][~]
$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/1.5.0.jdk
[13:56:57][~]
$ path
/Library/Java/JavaVirtualMachines/1.5.0.jdk/bin
/usr/local/bin
/usr/local/mysql/bin
/usr/bin
/bin
/usr/sbin
/sbin
/usr/local/bin
/usr/X11/bin
/Users/nicksieger/.rvm/bin
/Users/nicksieger/.gem/ruby/1.8/bin
/Users/nicksieger/Projects/ruby/jruby/bin/
/Users/nicksieger/bin
[13:57:02][~]
$ pickjdk
1) /Library/Java/JavaVirtualMachines/1.5.0.jdk 1.5.0.jdk < CURRENT
2) /Library/Java/JavaVirtualMachines/1.6.0-soylatte.jdk 1.6.0-soylatte.jdk
3) /Library/Java/JavaVirtualMachines/1.6.0_22-b04-307.jdk 1.6.0_22-b04-307.jdk
4) /Library/Java/JavaVirtualMachines/1.7.0.jdk 1.7.0.jdk
5) Soylatte-amd64
6) Soylatte16-i386-1.0.3
7) 1.7.0
8) None
Choose one of the above [1-8]: 8
[13:57:24][~]
$ echo $JAVA_HOME
[13:57:30][~]
$ :
[13:57:38][~]
$ path
/usr/local/bin
/usr/local/mysql/bin
/usr/bin
/bin
/usr/sbin
/sbin
/usr/local/bin
/usr/X11/bin
/Users/nicksieger/.rvm/bin
/Users/nicksieger/.gem/ruby/1.8/bin
/Users/nicksieger/Projects/ruby/jruby/bin/
/Users/nicksieger/bin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment