如何在Mac上更改Java的默认版本?

最佳答案

首先运行/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`


现在,当您运行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…行添加到外壳的init文件中。

对于Bash(如antonyh所述):

export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)

09-26 23:38