[Java] How do I switch between Java8, Java 7 and Java 6 on OS X

解決方法:

將以下程式碼加入到.bash_profile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
echo JAVA_HOME set to $JAVA_HOME
java -version
}
function removeFromPath() {
export PATH=$(echo $PATH | sed -E -e "s;:$1;;" -e "s;$1:?;;")
}

使用方式:

1
setjdk 1.8

參考資料:

How do I switch between Java 7 and
Java 6 on OS X 10.8.2?