Mac OS X和多个Java版本

本文介绍了在Mac OS X上安装和管理多个Java版本的解决方案,包括使用jEnv和homebrew-cask进行版本切换。建议卸载现有Java版本,安装所需版本,然后使用jEnv来管理这些版本。
摘要由CSDN通过智能技术生成

本文翻译自:Mac OS X and multiple Java versions

How can I install an additional java on MacOS? 如何在MacOS上安装其他Java? I installed jdk8 and that works fine. 我安装了jdk8,效果很好。 but now I need a jdk7 installation for development purposes. 但现在出于开发目的,我需要安装jdk7。 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. 当尝试通过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 达基


#1楼

参考:https://stackoom.com/question/1M9uT/Mac-OS-X和多个Java版本


#2楼

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. 然后,您只需在终端中键入java7java8即可切换版本。

(edit: updated to add Dylans improvement for Java 9) (编辑:已更新,以增加Dylans对Java 9的改进)


#3楼

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 java homebrew-cask安装Java版本
  • jenv to manage the installed versions of 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所示 ,这些是要遵循的步骤。

  1. install homebrew 安装自制软件
  2. install homebrew jenv 安装自制的Jenv
  3. install homebrew-cask 安装自制酒桶
  4. install a specific java version using cask (see "homebrew-cask versions" paragraph below) 使用桶安装特定的Java版本(请参见下面的“自制桶版本”一节)
  5. add this version for jenv to manage it 添加此版本供jenv管理
  6. check the version is correctly managed by jenv 检查版本是否由jenv正确管理
  7. repeat steps 4 to 6 for each version of java you need 对您需要的每个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 对于不同类型的JDK或安装,您将具有不同的路径

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? 您可以检查使用/usr/libexec/java_home -V安装的版本的路径,请参见如何检查Mac上是否安装了Java JDK?

On Mac OS X Mavericks, I found as following: 在Mac OS X Mavericks上,我发现如下:

1) Built-in JRE default: /Library/Internet\\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Home 1)内置JRE默认值: /Library/Internet\\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Home

2) JDKs downloaded from Apple: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/ 2)从Apple下载的JDK: /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 3)从Oracle下载的JDK: /Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home


Resources 资源资源


#4楼

I am using Mac OS X 10.9.5. 我正在使用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. 当我需要一个版本来运行应用程序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/$1/Contents/Home
   export PATH=$JAVA_HOME/bin:$PATH
  fi
 }
 function removeFromPath() {
  export PATH=$(echo $PATH | sed -E -e "s;:$1;;" -e "s;$1:?;;")
 }
#setjdk jdk1.8.0_60.jdk
setjdk jdk1.7.0_15.jdk

I put the above script in .profile file. 我把上面的脚本放在.profile文件中。 Just open terminal, type vi .profile, append the script with the above snippet and save it. 只需打开终端,输入vi .profile,将脚本附加以上代码片段并保存。 Once your out type source .profile , this will run your profile script without you having to restart the terminal. 一旦您输入source .profile ,它将运行您的配置文件脚本,而无需重新启动终端。 Now type java -version it should show 1.7 as your current version. 现在输入java -version它应该显示1.7作为当前版本。 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 . 如果打算将其更改为1.8,则注释行setjdk jdk1.7.0_15.jdk并取消注释行setjdk jdk1.8.0_60.jdk Save the script and run it again with source command. 保存该脚本,然后使用source命令再次运行它。 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. 当我必须编译2个需要不同Java版本的Maven项目时,我使用这种机制来管理JDK / JRE的多个版本。


#5楼

As found on this website So Let's begin by installing jEnv 正如在此网站上找到的,让我们从安装jEnv开始

  1. Run this in the terminal 在终端运行

     brew install https://raw.github.com/gcuisinier/jenv/homebrew/jenv.rb 
  2. Add jEnv to the bash profile 将jEnv添加到bash配置文件

     if which jenv > /dev/null; then eval "$(jenv init -)"; fi 
  3. When you first install jEnv will not have any JDK associated with it. 首次安装jEnv时,不会与它关联任何JDK。

    For example, I just installed JDK 8 but jEnv does not know about it. 例如,我刚刚安装了JDK 8,但是jEnv对此一无所知。 To check Java versions on jEnv 在jEnv上检查Java版本

    At the moment it only found Java version(jre) on the system. 目前,它仅在系统上找到Java版本(jre)。 The * shows the version currently selected. *显示当前选择的版本。 Unlike rvm and rbenv, jEnv cannot install JDK for you. 与rvm和rbenv不同,jEnv无法为您安装JDK。 You need to install JDK manually from Oracle website. 您需要从Oracle网站手动安装JDK。

  4. Install JDK 6 from Apple website. 从Apple网站安装JDK 6。 This will install Java in /System/Library/Java/JavaVirtualMachines/ . 这会将Java安装在/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. 我们从Apple网站安装Java 6的原因是SUN并未针对MAC提供JDK 6,因此Apple创建/修改了自己的部署版本。

  5. Similarly install JDK7 and JDK8. 同样,安装JDK7和JDK8。

  6. Add JDKs to jEnv. 将JDK添加到jEnv。

    JDK 6: JDK 6:

    JDK 7: JDK 7: http://javahabi@javahabit.com/wp-content/uploads/2015/03/img_5518ab9bc47d4.png

    JDK 8: JDK 8: http://javahabi@javahabit.com/wp-content/uploads/2015/03/img_5518abb2c1217.png

  7. Check the java versions installed using jenv 检查使用jenv安装的Java版本

    http://javahabi@javahabit.com/wp-content/uploads/2015/03/img_5518abceb0deb.png

  8. So now we have 3 versions of Java on our system. 因此,现在我们的系统上有3个Java版本。 To set a default version use the command 要设置默认版本,请使用以下命令

     jenv local <jenv version> 

    Ex – I wanted Jdk 1.6 to start IntelliJ 例–我想让Jdk 1.6启动IntelliJ

     jenv local oracle64-1.6.0.65 
  9. check the java version 检查java版本

    java -version Java版本 http://javahabi@javahabit.com/wp-content/uploads/2015/03/img_5518abe376dd0.png

That's it. 而已。 We now have multiple versions of java and we can switch between them easily. 现在,我们有多个Java版本,我们可以在它们之间轻松切换。 jEnv also has some other features, such as wrappers for Gradle, Ant, Maven, etc, and the ability to set JVM options globally or locally. jEnv还具有其他一些功能,例如用于Gradle,Ant,Maven等的包装器,以及能够在全局或本地设置JVM选项的功能。 Check out the documentation for more information. 查看文档以获取更多信息。


#6楼

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. 即使eval "$(jenv init -)"可以完成此工作。 The reason is /bin folder is not there anymore as describe in it's homepage, but shim folder is used as /bin instead. 原因是/ bin文件夹不再如其主页中所述,而是将shim文件夹用作/ bin。

  • Make sure ~/.jenv is there 确保〜/ .jenv在那
  • which java may print /Library/... which java可以打印/ Library / ...
  • jenv global 1.8 jenv全球1.8
  • jenv shell 1.8 jenv外壳1.8

Eventually, which java gives you: 最终, which java给您:

/Users/xxxx/.jenv/shims/java /用户/xxxx/.jenv/shims/java

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值