Mac OS X 和多个 Java 版本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26252591/
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
Mac OS X and multiple Java versions
提问by Dakkar
How can I install an additional java on MacOS? I installed jdk8 and that works fine. but now I need a jdk7 installation for development purposes. When trying to install the old version via DMG file, i get a warning, that there is already a newer version of java installed and the installer quits.
如何在 MacOS 上安装额外的 java?我安装了 jdk8,效果很好。但现在我需要一个 jdk7 安装用于开发目的。当尝试通过 DMG 文件安装旧版本时,我收到一条警告,指出已经安装了较新版本的 java 并且安装程序退出。
/usr/libexec/java_home -verbose
Matching Java Virtual Machines (1):
1.8.0_20, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_20.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/jdk1.8.0_20.jdk/Contents/Home
How to install jdk7 in addition to this one?
除了这个怎么安装jdk7?
Thanks
Dakky
由于
Dakky
采纳答案by Adrien Be
The cleanest way to manage multiple java versions on Mac is to use Homebrew
.
在 Mac 上管理多个 Java 版本的最简洁方法是使用Homebrew
.
And within Homebrew
, use:
在 中Homebrew
,使用:
homebrew-cask
to install the versions of javajenv
to manage the installed versions of java
homebrew-cask
安装java版本jenv
管理已安装的java版本
As seen on http://hanxue-it.blogspot.ch/2014/05/installing-java-8-managing-multiple.html, these are the steps to follow.
正如在http://hanxue-it.blogspot.ch/2014/05/installing-java-8-managing-multiple.html 上看到的,这些是要遵循的步骤。
- install homebrew
- install homebrew jenv
- install homebrew-cask
- install a specific java version using cask (see "homebrew-cask versions" paragraph below)
- add this version for jenv to manage it
- check the version is correctly managed by jenv
- repeat steps 4 to 6 for each version of java you need
- 安装自制软件
- 安装自制软件 jenv
- 安装自制酒桶
- 使用 cask 安装特定的 java 版本(请参阅下面的“homebrew-cask 版本”段落)
- 为 jenv 添加此版本以对其进行管理
- 检查版本是否由 jenv 正确管理
- 为您需要的每个 Java 版本重复步骤 4 到 6
homebrew-cask versions
自制酒桶版本
Add the homebrew/cask-versions
tap to homebrew using:
homebrew/cask-versions
使用以下命令将水龙头添加到自制软件:
brew tap homebrew/cask-versions
Then you can look at all the versions available:
然后你可以查看所有可用的版本:
brew search java
Then you can install the version(s) you like:
然后你可以安装你喜欢的版本:
brew cask install java7
brew cask install java6
And add them to be managed by jenv as usual.
并像往常一样添加它们由 jenv 管理。
jenv add <javaVersionPathHere>
I think this is the cleanest & simplest way to go about it.
我认为这是最干净和最简单的方法。
Another important thing to note, as mentioned in Mac OS X 10.6.7 Java Path Current JDK confusing:
另一个需要注意的重要事项,如Mac OS X 10.6.7 Java Path Current JDK 混淆中所述:
For different types of JDKs or installations, you will have different paths
You can check the paths of the versions installed using
/usr/libexec/java_home -V
, see How do I check if the Java JDK is installed on Mac?On Mac OS X Mavericks, I found as following:
1) Built-in JRE default:
/Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
2) JDKs downloaded from Apple:
/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/
3) JDKs downloaded from Oracle:
/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home
对于不同类型的 JDK 或安装,您将有不同的路径
您可以使用 来检查安装版本的路径
/usr/libexec/java_home -V
,请参阅如何检查 Mac 上是否安装了 Java JDK?在 Mac OS X Mavericks 上,我发现如下:
1)内置JRE默认:
/Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
2)从苹果下载的JDK:
/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/
3) 从 Oracle 下载的 JDK:
/Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home
Resources
资源
- Removing Java 8 JDK from Mac
- http://hanxue-it.blogspot.ch/2014/05/installing-java-8-managing-multiple.html
- http://sourabhbajaj.com/mac-setup/index.html
- http://brew.sh
- https://github.com/Homebrew/homebrew/tree/master/share/doc/homebrew#readme
- http://sourabhbajaj.com/mac-setup/Homebrew/README.html
- "brew tap” explained https://github.com/Homebrew/homebrew/blob/master/share/doc/homebrew/brew-tap.md
- “brew versions” explained Homebrew install specific version of formula?and also https://github.com/Homebrew/homebrew-versions
- https://github.com/caskroom/homebrew-cask
- “cask versions”, similar to “brew versions”, see https://github.com/caskroom/homebrew-versionsand also https://github.com/caskroom/homebrew-cask/issues/9447
- http://www.jenv.be
- https://github.com/gcuisinier/jenv
- 从 Mac 中删除 Java 8 JDK
- http://hanxue-it.blogspot.ch/2014/05/installing-java-8-managing-multiple.html
- http://sourabhbajaj.com/mac-setup/index.html
- http://brew.sh
- https://github.com/Homebrew/homebrew/tree/master/share/doc/homebrew#readme
- http://sourabhbajaj.com/mac-setup/Homebrew/README.html
- “brew tap”解释https://github.com/Homebrew/homebrew/blob/master/share/doc/homebrew/brew-tap.md
- “brew 版本”解释Homebrew 安装特定版本的公式?还有https://github.com/Homebrew/homebrew-versions
- https://github.com/caskroom/homebrew-cask
- “桶版本”,类似于“酿造版本”,参见https://github.com/caskroom/homebrew-versions和https://github.com/caskroom/homebrew-cask/issues/9447
- http://www.jenv.be
- https://github.com/gcuisinier/jenv
回答by Vegard
Uninstall jdk8, install jdk7, then reinstall jdk8.
卸载jdk8,安装jdk7,然后重新安装jdk8。
My approach to switching between them (in .profile) :
我在它们之间切换的方法(在 .profile 中):
export JAVA_7_HOME=$(/usr/libexec/java_home -v1.7)
export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8)
export JAVA_9_HOME=$(/usr/libexec/java_home -v9)
alias java7='export JAVA_HOME=$JAVA_7_HOME'
alias java8='export JAVA_HOME=$JAVA_8_HOME'
alias java9='export JAVA_HOME=$JAVA_9_HOME'
#default java8
export JAVA_HOME=$JAVA_8_HOME
Then you can simply type java7
or java8
in a terminal to switch versions.
然后你可以简单地输入java7
或java8
在终端中切换版本。
(edit: updated to add Dylans improvement for Java 9)
(编辑:更新为 Java 9 添加 Dylans 改进)
回答by ChaitanyaBhatt
I am using Mac OS X 10.9.5. This is how I manage multiple JDK/JRE on my machine when I need one version to run application A and use another version for application B.
我使用的是 Mac OS X 10.9.5。当我需要一个版本来运行应用程序 A 并为应用程序 B 使用另一个版本时,这就是我在我的机器上管理多个 JDK/JRE 的方式。
I created the following script after getting some help online.
在获得一些在线帮助后,我创建了以下脚本。
#!bin/sh
function setjdk() {
if [ $# -ne 0 ]; then
removeFromPath '/Library/Java/JavaVirtualMachines/'
if [ -n "${JAVA_HOME+x}" ]; then
removeFromPath $JAVA_HOME
fi
export JAVA_HOME=/Library/Java/JavaVirtualMachines//Contents/Home
export PATH=$JAVA_HOME/bin:$PATH
fi
}
function removeFromPath() {
export PATH=$(echo $PATH | sed -E -e "s;:;;" -e "s;:?;;")
}
#setjdk jdk1.8.0_60.jdk
setjdk jdk1.7.0_15.jdk
I put the above script in .profile file. Just open terminal, type vi .profile, append the script with the above snippet and save it. Once your out type source .profile
, this will run your profile script without you having to restart the terminal. Now type java -version
it should show 1.7 as your current version. If you intend to change it to 1.8 then comment the line setjdk jdk1.7.0_15.jdk
and uncomment the line setjdk jdk1.8.0_60.jdk
. Save the script and run it again with source command. I use this mechanism to manage multiple versions of JDK/JRE when I have to compile 2 different Maven projects which need different java versions.
我把上面的脚本放在 .profile 文件中。只需打开终端,输入 vi .profile,将上述代码片段附加到脚本中并保存。一旦您输入了source .profile
,这将运行您的配置文件脚本,而无需重新启动终端。现在输入java -version
它应该显示 1.7 作为您的当前版本。如果您打算将其更改为 1.8,请注释该行setjdk jdk1.7.0_15.jdk
并取消注释该行setjdk jdk1.8.0_60.jdk
。保存脚本并使用 source 命令再次运行它。当我必须编译需要不同 Java 版本的 2 个不同 Maven 项目时,我使用这种机制来管理 JDK/JRE 的多个版本。
回答by Dinesh Arora
As found on this websiteSo Let's begin by installing jEnv
Run this in the terminal
brew install https://raw.github.com/gcuisinier/jenv/homebrew/jenv.rb
Add jEnv to the bash profile
if which jenv > /dev/null; then eval "$(jenv init -)"; fi
When you first install jEnv will not have any JDK associated with it.
For example, I just installed JDK 8 but jEnv does not know about it. To check Java versions on jEnv
At the moment it only found Java version(jre) on the system. The
*
shows the version currently selected. Unlike rvm and rbenv, jEnv cannot install JDK for you. You need to install JDK manually from Oracle website.Install JDK 6 from Apple website. This will install Java in
/System/Library/Java/JavaVirtualMachines/
. The reason we are installing Java 6 from Apple website is that SUN did not come up with JDK 6 for MAC, so Apple created/modified its own deployment version.Similarly install JDK7 and JDK8.
Add JDKs to jEnv.
JDK 6:
Check the java versions installed using jenv
So now we have 3 versions of Java on our system. To set a default version use the command
jenv local <jenv version>
Ex – I wanted Jdk 1.6 to start IntelliJ
jenv local oracle64-1.6.0.65
check the java version
在终端运行这个
brew install https://raw.github.com/gcuisinier/jenv/homebrew/jenv.rb
将 jEnv 添加到 bash 配置文件
if which jenv > /dev/null; then eval "$(jenv init -)"; fi
当您第一次安装 jEnv 时,不会有任何与之关联的 JDK。
例如,我刚刚安装了 JDK 8,但 jEnv 不知道。在 jEnv 上检查 Java 版本
目前它只在系统上找到了 Java 版本(jre)。该
*
节目的当前版本选择。与 rvm 和 rbenv 不同,jEnv 无法为您安装 JDK。您需要从 Oracle 网站手动安装 JDK。从 Apple 网站安装 JDK 6。这将在
/System/Library/Java/JavaVirtualMachines/
. 我们从 Apple 网站安装 Java 6 的原因是 SUN 没有为 MAC 提供 JDK 6,因此 Apple 创建/修改了自己的部署版本。同样安装JDK7和JDK8。
将 JDK 添加到 jEnv。
JDK 6:
检查使用 jenv 安装的 java 版本
所以现在我们的系统上有 3 个版本的 Java。要设置默认版本,请使用命令
jenv local <jenv version>
例如——我想让 Jdk 1.6 启动 IntelliJ
jenv local oracle64-1.6.0.65
检查java版本
That's it. We now have multiple versions of java and we can switch between them easily. jEnv also has some other features, such as wrappers for Gradle, Ant, Maven, etc, and the ability to set JVM options globally or locally. Check out the documentation for more information.
就是这样。我们现在有多个版本的 java,我们可以轻松地在它们之间切换。jEnv 还具有一些其他功能,例如 Gradle、Ant、Maven 等的包装器,以及全局或本地设置 JVM 选项的能力。查看文档以获取更多信息。
回答by CodeFarmer
Jenv on Mac Sierra:
Mac Sierra 上的 Jenv:
if not working after install, do this bug fix to add java executable to path
如果安装后不起作用,请执行此错误修复以将 java 可执行文件添加到路径
export PATH="$HOME/.jenv/shims:$PATH"
even though eval "$(jenv init -)"
could do this job. The reason is /bin folder is not there anymore as describe in it's homepage, but shim folder is used as /bin instead.
即使eval "$(jenv init -)"
能胜任这份工作。原因是 /bin 文件夹不再存在,如其主页中所述,而是将 shim 文件夹用作 /bin 。
- Make sure ~/.jenv is there
which java
may print /Library/...- jenv global 1.8
- jenv shell 1.8
- 确保 ~/.jenv 在那里
which java
可以打印 /Library/...- jenv 全球 1.8
- jenv 外壳 1.8
Eventually, which java
gives you:
最终,which java
给你:
/Users/xxxx/.jenv/shims/java
/Users/xxxx/.jenv/shims/java
回答by York Yang
I find this Java version manager called Jabbarecently and the usage is very similar to version managers of other languages like rvm(ruby), nvm(node), pyenv(python), etc. Also it's cross platform so definitely it can be used on Mac.
我最近发现这个名为Jabba 的Java 版本管理器的用法与其他语言的版本管理器非常相似,如 rvm(ruby)、nvm(node)、pyenv(python) 等。而且它是跨平台的,所以肯定可以在苹果电脑。
After installation, it will create a dir in ~/.jabba to put all the Java versions you install. It "Supports installation of Oracle JDK (default) / Server JRE, Zulu OpenJDK (since 0.3.0), IBM SDK, Java Technology Edition (since 0.6.0) and from custom URLs.".
安装后,它会在 ~/.jabba 中创建一个目录来放置您安装的所有 Java 版本。它“支持安装 Oracle JDK(默认)/Server JRE、Zulu OpenJDK(自 0.3.0 起)、IBM SDK、Java 技术版(自 0.6.0 起)和来自自定义 URL。”。
Basic usage is listed on their Github. A quick summary to start:
他们的 Github 上列出了基本用法。快速总结开始:
curl -sL https://github.com/shyiko/jabba/raw/master/install.sh | bash && . ~/.jabba/jabba.sh
# install Oracle JDK
jabba install 1.8 # "jabba use 1.8" will be called automatically
jabba install 1.7 # "jabba use 1.7" will be called automatically
# list all installed JDK's
jabba ls
# switch to a different version of JDK
jabba use 1.8
回答by Michael Easter
SDKMAN!is a great tool for using multiple versions of Java, Gradle, Groovy, Kotlin, and other JVM tools on Mac OS. Installation and usage doc are plainly on the main site.
斯凯曼!是在 Mac OS 上使用多个版本的 Java、Gradle、Groovy、Kotlin 和其他 JVM 工具的绝佳工具。安装和使用文档在主站点上很清楚。
(I have no affiliation, just a happy user).
(我没有从属关系,只是一个快乐的用户)。
As an example usage, if I type the following in a Terminal window, there is a list of available Java SDK versions (edited for brevity):
作为示例用法,如果我在终端窗口中键入以下内容,则会有一个可用 Java SDK 版本列表(为简洁起见进行了编辑):
$ sdk list java
Available Java Versions
+ 9ea170
> + 8u131
7u141-zulu
Here +
denotes that the version is installed. >
denotes which version is currently in use. To install a version:
这里+
表示安装了版本。>
表示当前使用的是哪个版本。要安装一个版本:
$ sdk install java 7u141-zulu
To use a version in this Terminal window:
要在此终端窗口中使用某个版本:
$ sdk use java 9ea170
回答by bob
For macOS Sierra 420
对于 macOS Sierra 420
This guide was cobbled together from various sources (replies above as well as other posts), and works perfect.
本指南是从各种来源(上面的回复以及其他帖子)拼凑而成的,并且效果很好。
0. If you haven't already, install homebrew.
0. 如果你还没有安装自制软件。
See https://brew.sh/
1. Install jenv
1. 安装 jenv
brew install jenv
2. Add jenv to the bash profile
2.在bash配置文件中添加jenv
if which jenv > /dev/null; then eval "$(jenv init -)"; fi
3. Add jenv to your path
3. 将 jenv 添加到您的路径中
export PATH="$HOME/.jenv/shims:$PATH"
4. Tap "caskroom/versions"
4. 点击“化装室/版本”
FYI: "Tap" extends brew's list of available repos it can install, above and beyond brew's default list of available repos.
仅供参考:“点击”扩展了 brew 可以安装的可用存储库列表,超出了 brew 的默认可用存储库列表。
brew tap caskroom/versions
5. Install the latest version of java
5.安装最新版本的java
brew cask install java
6. Install java 6 (or 7 or 8 whatever you need)
6. 安装 java 6(或 7 或 8 任何你需要的)
brew cask install java6
#brew cask install java7
#brew cask install java8
? Maybe close and restart Terminal so it sees any new ENV vars that got setup.
? 也许关闭并重新启动终端,以便它看到任何已设置的新 ENV 变量。
7. Review Installations
7. 检查安装
All Java version get installed here: /Library/Java/JavaVirtualMachines
lets take a look.
所有 Java 版本都在这里安装:/Library/Java/JavaVirtualMachines
让我们来看看。
ls -la /Library/Java/JavaVirtualMachines
8. Add each path to jenv one-at-a-time.
8. 一次将每个路径添加到 jenv。
We need to add "/Contents/Home" to the version folder. WARNING: Use the actual paths on your machine... these are just EXAMPLE's
我们需要将“/Contents/Home”添加到版本文件夹中。警告:使用您机器上的实际路径...这些只是示例的
jenv add /Library/Java/JavaVirtualMachines/1.6.0___EXAMPLE___/Contents/Home
jenv add /Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk___EXAMPLE___/Contents/Home
9. Check if jenv registered OK
9.检查jenv是否注册OK
jenv versions
10. Set java version to use (globably)
10.设置要使用的java版本(全局)
Where XX matches one of the items in the versions list above.
其中 XX 匹配上面版本列表中的项目之一。
jenv global XX
Check java version
检查Java版本
java -version
Check jenv versions
检查 jenv 版本
Should also indicate the current version being used with an asterisk.
还应该用星号指示正在使用的当前版本。
jenv versions
DONE
完毕
Quick future reference
未来快速参考
To change java versions
更改 Java 版本
... See the list of available java versions
... 查看可用 Java 版本列表
jenv versions
... then, where XX matches an item in the list above
...然后,其中 XX 匹配上面列表中的一个项目
jenv global XX
回答by elad.chen
Here's a more DRY version for bash (Based on Vegard's answer)
这是 bash 的更 DRY 版本(基于 Vegard 的回答)
Replace 1.7 and 1.8 with whatever versions you are interested with and you'll get an alias called 'javaX'; where 'X' is the java version (7 / 8 in the snippet below) that will allow you to easily switch versions
将 1.7 和 1.8 替换为您感兴趣的任何版本,您将获得一个名为“javaX”的别名;其中“X”是 Java 版本(以下代码段中的 7 / 8),可让您轻松切换版本
for version in 1.7 1.8; do
v="${version: -1}"
h=JAVA_"$v"_HOME
export "$h"=$(/usr/libexec/java_home -v $version)
alias "java$v"="export JAVA_HOME=$$h"
done
回答by hulius
In the same spirit than @Vegard (lightweight):
本着与@Vegard(轻量级)相同的精神:
- Install the wanted JDKs with Homebrew
Put this
jdk
bash function and a default in your.profile
jdk() { version= export JAVA_HOME=$(/usr/libexec/java_home -v"$version"); java -version } export JAVA_HOME=$(/usr/libexec/java_home -v11); # Your default version
and then, to switch your jdk, you can do
jdk 9 jdk 11 jdk 13
- 使用 Homebrew 安装所需的 JDK
把这个
jdk
bash 函数和一个默认值放在你的.profile
jdk() { version= export JAVA_HOME=$(/usr/libexec/java_home -v"$version"); java -version } export JAVA_HOME=$(/usr/libexec/java_home -v11); # Your default version
然后,要切换你的 jdk,你可以这样做
jdk 9 jdk 11 jdk 13