Created
November 9, 2008 03:35
-
-
Save nicksieger/23190 to your computer and use it in GitHub Desktop.
pickjdk: a JDK chooser bash function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# pickjdk: provides a shell function that allows you to choose a JDK. | |
# | |
# The function sets JAVA_HOME and PATH On Linux or | |
# Windows, 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. On OSX, JDKs will be discovered for you. | |
# JDKS_ROOT=/opt/jdk | |
findjdks() | |
{ | |
JDKS=( ) | |
VERSIONS=( ) | |
# Use /usr/libexec/java_home on mac to find installed JDKs | |
if [ -x /usr/libexec/java_home ]; then | |
local pairs=( $(/usr/libexec/java_home -a x86_64 -V 2>&1 \ | |
| sort | awk -F'\t' '(NF==3){split($1,a,","); print a[1] ":" $3}') ) | |
for p in ${pairs[@]}; do | |
JDKS[${#JDKS[@]}]=${p##*:} | |
VERSIONS[${#VERSIONS[@]}]=${p%%:*} | |
done | |
else | |
for jdk in $JDKS_ROOT/*; do | |
local version=$(basename $jdk) | |
if [ ! -x $jdk/bin/java ]; then | |
continue | |
fi | |
JDKS[${#JDKS[@]}]=$jdk | |
VERSIONS[${#VERSIONS[@]}]=$version | |
done | |
fi | |
[ ${#JDKS[@]} -ne 0 ] | |
} | |
pickjdk() | |
{ | |
## Load JDKs | |
if ! findjdks; then | |
echo 'error: no jdks found!' 1>&2 | |
return 1 | |
fi | |
## Add 'None' menu item | |
JDKS[${#JDKS[@]}]=None | |
VERSIONS[${#VERSIONS[@]}]=None | |
local i= currjdk=$JAVA_HOME choice= | |
## Print menu in interactive mode (no args) | |
for n in $(seq 0 $[ ${#JDKS[@]} - 1 ]); do | |
i=$[ $n + 1 ] | |
if [ -z "$1" ]; then | |
echo -n " $i) ${VERSIONS[$n]}" | |
if [ "${JDKS[$n]}" = "$currjdk" ]; then | |
echo " < CURRENT" | |
else | |
echo | |
fi | |
fi | |
done | |
## Determine JDK to set up | |
if [ "$1" ]; then # Process command-line choice | |
if [ -z "${1/[0-9]/}" ] && [ $1 -le ${#JDKS[@]} ]; then | |
# Simple integer - menu choice | |
choice=$[ $1 - 1 ] | |
fi | |
if [ -z "$choice" ]; then | |
# Match against version names. | |
# Use reverse order to pick most recent JDK for matching version | |
for n in $(seq $[ ${#JDKS[@]} - 1 ] 0); do | |
if [ "${VERSIONS[$n]/$1/}" != ${VERSIONS[$n]} ]; then | |
choice=$n | |
break | |
fi | |
done | |
fi | |
if [ -z "$choice" ]; then | |
echo "error: could not find JDK matching '$1'" 1>&2 | |
return 1 | |
fi | |
elif [ ${#JDKS[@]} -gt 1 ]; then # Read menu choice | |
choice=${#JDKS[@]} | |
while [ -z "${JDKS[$choice]}" ]; do | |
echo -n "Choose one of the above [1-${#JDKS[@]}]: " | |
read choice | |
[ -z "${choice/[0-9]/}" ] && choice=$[ $choice - 1 ] | |
done | |
else # Only one JDK so we don't have a choice | |
choice=0 | |
fi | |
[ -z "$currjdk" ] && currjdk=$(dirname $(dirname $(type -path java))) | |
## Set (or unset) JAVA_HOME | |
if [ ${JDKS[$choice]} != None ]; then | |
export JAVA_HOME=${JDKS[$choice]} | |
else | |
unset JAVA_HOME | |
fi | |
## Set or replace JDK in PATH | |
local unset_jdk= | |
for jdk in ${JDKS[*]}; do | |
if [ "$currjdk" = "$jdk" ]; then | |
unset_jdk=$jdk | |
break | |
fi | |
done | |
if [ "$unset_jdk" ]; then | |
if [ -z "$JAVA_HOME" ]; then | |
PATH=$(echo $PATH | sed "s|$unset_jdk/bin:*||g") | |
else | |
PATH=$(echo $PATH | sed "s|$unset_jdk|$JAVA_HOME|g") | |
fi | |
elif [ "$JAVA_HOME" ]; then | |
PATH="$JAVA_HOME/bin:$PATH" | |
fi | |
## Announce new version and clean up | |
[ "${VERSIONS[$choice]}" != None ] && echo "New JDK: ${VERSIONS[$choice]}" | |
hash -r | |
unset JDKS VERSIONS | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment