Skip to content

Instantly share code, notes, and snippets.

@sylvainmathieu
Created January 13, 2015 16:40
Show Gist options
  • Save sylvainmathieu/e846e85f847215096ebc to your computer and use it in GitHub Desktop.
Save sylvainmathieu/e846e85f847215096ebc to your computer and use it in GitHub Desktop.
Switch easily between JDKs on OSx (to include in ~/.profile)
# Usage: `setjdk 1.7` or `setjdk 1.8`
function removeFromPath() {
export PATH=$(echo $PATH | sed -E -e "s;:$1;;" -e "s;$1:?;;")
}
function setjdk() {
if [ $# -ne 0 ]; then
removeFromPath '/System/Library/Frameworks/JavaVM.framework/Home/bin'
if [ -n "${JAVA_HOME+x}" ]; then
removeFromPath $JAVA_HOME
fi
export JAVA_HOME=`/usr/libexec/java_home -v $@`
export PATH=$JAVA_HOME/bin:$PATH
if [ -e "$SET_JAVA_HOME_FILE" ]; then
rm $SET_JAVA_HOME_FILE
fi
touch $SET_JAVA_HOME_FILE
echo $JAVA_HOME >> $SET_JAVA_HOME_FILE
java -version
fi
}
# Set current JAVA_HOME
SET_JAVA_HOME_FILE='/Users/sma/.set_java_home'
export JAVA_HOME=`cat $SET_JAVA_HOME_FILE`
if [ -n "$1" ]; then
export PATH=$JAVA_HOME/bin:$PATH
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment