#!/bin/bash## Provides a function that allows you to choose a JDK. Just set the environment # variable JDKS_ROOT to the directory containing multiple versions of the JDK# and the function will prompt you to select one. JAVA_HOME and PATH will be cleaned# up and set appropriately._macosx(){
if [ $(uname -s) = Darwin ];then
return 0
else
return 1
fi
}
JDKS_ROOT=
if _macosx;then
JDKS_ROOT=/System/Library/Frameworks/JavaVM.framework/Versions
fipickjdk(){
if [ -z "$JDKS_ROOT" ];then
return 1
fi
declare -a JDKS
local n=1 jdk total_jdks choice=0 currjdk=$JAVA_HOME explicit_jdk
for jdk in$JDKS_ROOT/[0-9s]*;doif [ -d $jdk -a ! -L $jdk ];then
echo -n " $n) $(basename $jdk)"if _macosx;then
jdk=$jdk/Home
fiif [ $jdk = "$currjdk" ];then
echo " < CURRENT"else
echo
fi
JDKS[$n]=$jdk
total_jdks=$n
n=$[ $n + 1 ]
fidone
echo " $n) None"
JDKS[$n]=None
total_jdks=$nif [ $total_jdks -gt 1 ];thenwhile [ -z "${JDKS[$choice]}" ];do
echo -n "Choose one of the above [1-$total_jdks]: "
read choice
doneelse
choice=1
fiif [ -z "$currjdk" ];then
currjdk=$(dirname $(dirname $(type -path java)))fiif [ ${JDKS[$choice]}!= None ];thenexport JAVA_HOME=${JDKS[$choice]}else
unset JAVA_HOME
fi
explicit_jdk=
for jdk in${JDKS[*]};doif [ "$currjdk" = "$jdk" ];then
explicit_jdk=$jdk
break
fidoneif [ "$explicit_jdk" ];thenif [ -z "$JAVA_HOME" ];then
PATH=$(echo $PATH| sed "s|$explicit_jdk/bin:*||g")else
PATH=$(echo $PATH| sed "s|$explicit_jdk|$JAVA_HOME|g")fielif [ "$JAVA_HOME" ];then
PATH="$JAVA_HOME/bin:$PATH"fi
hash -r
unset JDKS
}