如何在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

除了这个,如何安装jdk7 ?


当前回答

在Mac上管理多个java版本最简单的方法是使用Homebrew。

在Homebrew中,使用:

Homebrew-cask来安装Java版本 Jenv来管理已安装的Java版本


如http://hanxue-it.blogspot.ch/2014/05/installing-java-8-managing-multiple.html所示,以下是需要遵循的步骤。

安装自酿酒 安装自制jenv 安装homebrew-cask 使用cask安装特定的Java版本(请参阅下面的“家酿-cask版本”段落) 添加这个版本给jenv来管理它 检查版本是否由jenv正确管理 对于您需要的每个Java版本,重复步骤4到6


homebrew-cask版本

添加自制/桶版本的点击到自制,使用:

brew tap homebrew/cask-versions

然后你可以查看所有可用的版本:

brew search java

然后你可以安装你喜欢的版本:

brew install --cask java7
brew install --cask java6

并像往常一样将它们添加到jenv管理。

jenv add <javaVersionPathHere>

我认为这是最干净和最简单的方法。


还有一件重要的事情需要注意,正如在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: Built-in JRE default: /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Home JDKs downloaded from Apple: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/ JDKs downloaded from Oracle: /Library/Java/JavaVirtualMachines/jdk1.8.0_11.jdk/Contents/Home


资源

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-versions and also https://github.com/caskroom/homebrew-cask/issues/9447 http://www.jenv.be https://github.com/gcuisinier/jenv

其他回答

要查找可用的Java版本:

brew search java

要安装最新的稳定版本(截至今天有19个):

brew info java
brew install java

为了让系统Java包装器(例如:ide)找到最新的JDK,可以将其与以下符号链接:

sudo ln -sfn /opt/homebrew/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk

安装特定版本(11):

brew install java11

对于系统,Java包装器查找JDK 11:

sudo ln -sfn /opt/homebrew/opt/openjdk@11/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-11.jdk

您可以在PATH中拥有其中一个OpenJDK版本。在~/.bash_profile中添加下列行之一

export PATH="/opt/homebrew/opt/openjdk/bin:$PATH
export PATH="/opt/homebrew/opt/openjdk@11/bin:$PATH

然后,

source ~/.bash_profile

我最近发现了一个叫做Jabba的Java版本管理器,它的用法与其他语言的版本管理器非常相似,比如rvm(ruby)、nvm(node)、pyenv(python)等。而且它是跨平台的,所以肯定可以在Mac上使用。

安装完成后,它将在~/目录下创建一个目录。jabba来放置你安装的所有Java版本。它“支持安装Oracle JDK(默认)/ Server JRE, Zulu OpenJDK(自0.3.0起),IBM SDK, Java技术版(自0.6.0起)和自定义url。”

他们的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

我使用的是Mac OS X 10.9.5。这就是当我需要一个版本运行应用程序A,而应用程序B使用另一个版本时,我如何在我的机器上管理多个JDK/JRE。

我在网上得到一些帮助后创建了以下脚本。

#!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. 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.

我按照下面的步骤链接- https://medium.com/@euedofia/fix-default- java-versionon -maven-on-mac-os-x-156cf5930078,它对我有效。

cd /usr/local/Cellar/maven/3.5.4/bin/
nano mvn
--Update JAVA_HOME -> "${JAVA_HOME:-$(/usr/libexec/java_home)}"
mvn -version

卸载jdk8,安装jdk7,然后重新安装jdk8。

我在它们之间切换的方法(在.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

然后,只需在终端中输入java7或java8即可切换版本。

(编辑:更新为Java 9添加了dylan的改进)