Skip to content

Instantly share code, notes, and snippets.

@buckett
Last active January 6, 2021 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save buckett/5832311 to your computer and use it in GitHub Desktop.
Save buckett/5832311 to your computer and use it in GitHub Desktop.
set-java(1) for OS X
#!/bin/bash
# Set the current version of Java being used.
# Doesn't at the moment set the path as the mac just pulls them from JAVA_HOME
# Get the current
current=$JAVA_HOME
# Look for the default
default=$(/usr/libexec/java_home)
# Get a list of all the options
# Output is path:name:version
options=$(/usr/libexec/java_home -V 2>&1 | grep '^ ' | while read line; do path=$(echo ${line##* }); name=$(echo ${line% *}); echo -e "${name}:${path}"; done | sort -n)
echo "$options" | while read line
do
name=$(echo $line| cut -d: -f1)
path=$(echo $line| cut -d: -f2| tr -d '"')
if [ "$path" == "$current" ]; then
name="${name} (*)"
fi
echo -e "$name\t( $path )"
done | cat -n 1>&2
echo -n "Select your JDK: " 1>&2
read line
if [ -n "$line" ]; then
# Return the selected path on stdout
echo "$options" | cut -d: -f2| sed -n "${line}p"
else
echo "$current"
fi
@buckett
Copy link
Author

buckett commented Jun 21, 2013

To use this with bash set the alias:

alias set-java='export JAVA_HOME=$(/Users/buckett/bin/set-java)'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment