Skip to content

Instantly share code, notes, and snippets.

@thomashartm
Last active August 29, 2015 14:23
Show Gist options
  • Save thomashartm/1e4d2b3cc9dd773d1a86 to your computer and use it in GitHub Desktop.
Save thomashartm/1e4d2b3cc9dd773d1a86 to your computer and use it in GitHub Desktop.
Switch JDK version bash command for Mac OSX
#!/bin/bash
####################################
# Exports jdk environment exports
# call:
# set-jdk <jdk-version> e.g. set-jdk 1.8
# or to set default:
# set-jdk
####################################
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
fi
}
function removeFromPath() {
export PATH=$(echo $PATH | sed -E -e "s;:$1;;" -e "s;$1:?;;")
}
if [ "$1" == "1.8" ]; then
echo -e "Set JDK Version to 1.8"
# sets export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8)
setjdk 1.8
else
#sets export JAVA_7_HOME=$(/usr/libexec/java_home -v1.7)
echo -e "Set JDK Version to 1.7"
setjdk 1.7
fi
echo -e "Execute: set-jdk <jdk-version> e.g. set-jdk 1.8 to change version"
@thomashartm
Copy link
Author

Place the script somewhere in your path and run chmod a+x set-jdk to make it executable.

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