本文翻译自:How to set or change the default Java (JDK) version on OS X?
如何在Mac上更改Java的默认版本?
#1楼
参考:https://stackoom.com/question/1UA1V/如何在OS-X上设置或更改默认Java-JDK-版本
#2楼
It is a little bit tricky, but try to follow the steps described in Installing Java on OS X 10.9 (Mavericks) . 这有点棘手,但是请尝试按照在OS X 10.9上安装Java(Mavericks)中描述的步骤进行操作。 Basically, you gonna have to update your alias to java
. 基本上,您必须将别名更新为java
。
Step by step: 一步步:
After installing JDK 1.7, you will need to do the sudo ln -snf
in order to change the link to current java
. 安装JDK 1.7之后,您需要执行sudo ln -snf
才能将链接更改为当前java
。 To do so, open Terminal and issue the command: 为此,请打开“终端”并发出以下命令:
sudo ln -nsf /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents \
/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK
Note that the directory jdk1.7.0_51.jdk
may change depending on the SDK version you have installed. 请注意,目录jdk1.7.0_51.jdk
可能会根据您安装的SDK版本而更改。
Now, you need to set JAVA_HOME
to point to where jdk_1.7.0_xx.jdk
was installed. 现在,您需要将JAVA_HOME
为指向jdk_1.7.0_xx.jdk
安装位置。 Open again the Terminal and type: 再次打开终端,然后键入:
export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home"
You can add the export JAVA_HOME
line above in your .bashrc
file to have java permanently in your Terminal 您可以在.bashrc
文件中添加上面的export JAVA_HOME
行,以在终端中永久拥有Java
#3楼
First run /usr/libexec/java_home -V
which will output something like the following: 首先运行/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
Pick the version you want to be the default ( 1.6.0_65-b14-462
for arguments sake) then: 选择您想要成为默认版本的版本(为参数着想,版本为1.6.0_65-b14-462
),然后:
export JAVA_HOME=`/usr/libexec/java_home -v 1.6.0_65-b14-462`
or you can specify just the major version, like: 或者您可以仅指定主要版本,例如:
export JAVA_HOME=`/usr/libexec/java_home -v 1.8`
Now when you run java -version
you will see: 现在,当您运行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)
Add the export JAVA_HOME…
line to your shell's init file. 将export JAVA_HOME…
行添加到Shell的init文件中。
For Bash (as stated by antonyh ): 对于Bash(如antonyh所述 ):
export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)
#4楼
This tool will do the work for you: 该工具将为您完成工作:
http://www.guigarage.com/2013/02/change-java-version-on-mac-os/ http://www.guigarage.com/2013/02/change-java-version-on-mac-os/
It's a simple JavaOne that can be used to define the current Java Version. 这是一个简单的JavaOne,可用于定义当前的Java版本。 The version can be used in a shell that is opened after a version was selected in the tool. 可以在工具中选择版本后打开的外壳中使用该版本。
#5楼
From the Apple's official java_home(1) man page : 从苹果官方的java_home(1)手册页 :
**USAGE**
/usr/libexec/java_home helps users set a $JAVA_HOME in their login rc files, or provides a way for
command-line Java tools to use the most appropriate JVM which can satisfy a minimum version or archi-
tecture requirement. The --exec argument can invoke tools in the selected $JAVA_HOME/bin directory,
which is useful for starting Java command-line tools from launchd plists without hardcoding the full
path to the Java command-line tool.
Usage for bash-style shells:
$ export JAVA_HOME=`/usr/libexec/java_home`
Usage for csh-style shells:
% setenv JAVA_HOME `/usr/libexec/java_home`
#6楼
如果您有多个版本,并且想要使用特定版本来运行某些版本,请使用以下示例:
/usr/libexec/java_home -v 1.7.0_75 --exec java -jar you-file.jar