如何在 OS X 上设置或更改默认的 Java (JDK) 版本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21964709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set or change the default Java (JDK) version on OS X?
提问by Venkat
How can you change the default version of Java on a mac?
如何在 Mac 上更改 Java 的默认版本?
回答by Trein
It is a little bit tricky, but try to follow the steps described in Installing Java on OS X 10.9 (Mavericks). Basically, you gonna have to update your alias to java
.
这有点棘手,但请尝试按照在 OS X 10.9 (Mavericks) 上安装 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
. To do so, open Terminal and issue the command:
安装 JDK 1.7 后,您需要执行sudo ln -snf
以将链接更改为当前java
. 为此,请打开终端并发出命令:
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. Open again the Terminal and type:
现在,您需要设置JAVA_HOME
为指向jdk_1.7.0_xx.jdk
安装位置。再次打开终端并输入:
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
您可以export JAVA_HOME
在.bashrc
文件中添加上面的行,以便在终端中永久使用 java
回答by markhellewell
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)
For Fish (as stated by ormurin)
对于鱼(如 ormurin 所述)
set -x JAVA_HOME (/usr/libexec/java_home -d64 -v1.8)
回答by Hendrik Ebbers
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. The version can be used in a shell that is opened after a version was selected in the tool.
它是一个简单的 JavaOne,可用于定义当前的 Java 版本。该版本可用于在工具中选择版本后打开的外壳。
回答by Antonio
From the Apple's official java_home(1) man page:
来自 Apple 的官方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`
回答by MonstroDev
If you have multiple versions and you want to run something by using a specific version, use this example:
如果您有多个版本并且想要使用特定版本运行某些内容,请使用以下示例:
/usr/libexec/java_home -v 1.7.0_75 --exec java -jar you-file.jar
回答by Yuantao
Use jenv is an easy way.
使用 jenv 是一种简单的方法。
1.Install jenv
1.安装jenv
curl -s get.jenv.io | bash
2.Config jenv
2.配置jenv
cd ~/.jenv/candidates/ mkdir java cd java mkdir 1.7 mkdir 1.8
3.Symlink the jdk path
3.符号链接jdk路径
ln -s /Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/bin ~/.jenv/candidates/java/1.7 ln -s /Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home/bin ~/.jenv/candidates/java/1.8
4.You are all set
4.你都准备好了
switch command:
切换命令:
jenv use java 1.8
jenv 使用 java 1.8
set default:
默认设置:
jenv default java 1.7
jenv 默认 java 1.7
回答by kenglxn
A small fish function based on /usr/libexec/java_home
基于/usr/libexec/java_home的小鱼函数
function jhome
set JAVA_HOME (/usr/libexec/java_home $argv)
echo "JAVA_HOME:" $JAVA_HOME
echo "java -version:"
java -version
end
If you don't use fish, you can do something similar in bash:
如果你不使用 fish,你可以在 bash 中做类似的事情:
#!/bin/bash
jhome () {
export JAVA_HOME=`/usr/libexec/java_home $@`
echo "JAVA_HOME:" $JAVA_HOME
echo "java -version:"
java -version
}
Then to switch between javas do:
然后在javas之间切换:
$> jhome #switches to latest java
$> jhome -v 1.7 #switches to java 1.7
$> jhome -v 1.6 #switches to java 1.6
回答by user2708060
install JDK, not just JRE
安装JDK,而不仅仅是JRE
/usr/libexec/java_home -v 1.8
gives
给
/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home
next
下一个
touch .bash_profile
open -a TextEdit.app .bash_profile
TextEdit will show you a blank page which you can fill in.
TextEdit 将显示一个空白页面,您可以填写该页面。
add to doc:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home
添加到文档:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home
in terminal:
在终端:
export JAVA_HOME="$(/usr/libexec/java_home -v 1.8)"
try the command:
试试命令:
javac -version
javac -version
should output:
应该输出:
javac 1.8.0_111
回答by Justin Zhang
Adding to the above answers, I put the following lines in my .bash_profile
(or .zshrc
for MacOS 10.15+) which makes it really convenient to switch (including @elektromin's comment for java 9):
除了上述答案之外,我在我的.bash_profile
(或.zshrc
MacOS 10.15+)中添加了以下几行,这使得切换非常方便(包括 @elektromin 对 java 9 的评论):
alias j12="export JAVA_HOME=`/usr/libexec/java_home -v 12`; java -version"
alias j11="export JAVA_HOME=`/usr/libexec/java_home -v 11`; java -version"
alias j10="export JAVA_HOME=`/usr/libexec/java_home -v 10`; java -version"
alias j9="export JAVA_HOME=`/usr/libexec/java_home -v 9`; java -version"
alias j8="export JAVA_HOME=`/usr/libexec/java_home -v 1.8`; java -version"
alias j7="export JAVA_HOME=`/usr/libexec/java_home -v 1.7`; java -version"
After inserting, execute $ source .bash_profile
插入后执行 $ source .bash_profile
I can switch to Java 8 by typing the following:
我可以通过键入以下内容切换到 Java 8:
$ j8
java version "1.8.0_102"
Java(TM) SE Runtime Environment (build 1.8.0_102-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.102-b14, mixed mode)
回答by Alex
Based on @markhellewell answer I created a couple of alias functions that will do it for you. Just add these to your shell startup file
基于@markhellewell 的回答,我创建了几个别名函数来为你做这件事。只需将这些添加到您的 shell 启动文件中
#list available jdks
alias jdks="/usr/libexec/java_home -V"
# jdk version switching - e.g. `jdk 6` will switch to version 1.6
function jdk() {
echo "Switching java version";
export JAVA_HOME=`/usr/libexec/java_home -v 1.`;
java -version;
}
https://gist.github.com/Noyabronok/0a90e1f3c52d1aaa941013d3caa8d0e4
https://gist.github.com/Noyabronok/0a90e1f3c52d1aaa941013d3caa8d0e4