Skip to content

Instantly share code, notes, and snippets.

@practice
Created September 2, 2014 08:38
Show Gist options
  • Save practice/ba7f50c76d3c5a733519 to your computer and use it in GitHub Desktop.
Save practice/ba7f50c76d3c5a733519 to your computer and use it in GitHub Desktop.
Switch jdk 6,7,8 from terminal in Mac OS X
# refer http://www.jayway.com/2014/01/15/how-to-switch-jdk-version-on-mac-os-x-maverick/
#creating a special home for Java 8
export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8)
#creating a special home for Java 7
export JAVA_7_HOME=$(/usr/libexec/java_home -v1.7)
#creating a special home for Java 6
export JAVA_6_HOME=$(/usr/libexec/java_home -v1.6)
#making Java8 as our default java for the system
export JAVA_HOME=$JAVA_7_HOME
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:?;;")
}
setjdk 1.8
#Command line alias so we can switch to java6 or Java7
# alias java6='export JAVA_HOME=$JAVA_6_HOME'
# alias java7='export JAVA_HOME=$JAVA_7_HOME'
# alias java8='export JAVA_HOME=$JAVA_7_HOME'
alias java6='setjdk 1.6'
alias java7='setjdk 1.7'
alias java8='setjdk 1.8'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment