如何在OS X上设置或更改默认的Java(JDK)版本?
首次运行/usr/libexec/java_home -V将输出如下内容:
Matching Java Virtual Machines (3):
1.8.0_05, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home
1.6.0_65-b14-462, x86_64: "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
1.6.0_65-b14-462, i386: "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home
选择您想要的默认版本(1.6.0_65-b14-462为了参数),然后:
export JAVA_HOME=`/usr/libexec/java_home -v 1.6.0_65-b14-462`
或者指定主要版本,例如:
export JAVA_HOME=`/usr/libexec/java_home -v 1.8`
在terminal中输入 java -version你会看到:
java version "1.6.0_65"
Java(TM) SE Runtime Environment (build 1.6.0_65-b14-462-11M4609)
Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-462, mixed mode)
只需将该export JAVA_HOME…行添加到shell的init文件中即可。
对于Bash(如antonyh所述):
export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)