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

其他回答

卸载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的改进)

在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版本。 打开一个新的终端窗口并输入:

/usr/libexec/java_home -V

你的输出应该是这样的:

Matching Java Virtual Machines (2):
11.0.1, x86_64: "Java SE 11.0.1" /Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home
1.8.0_201, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home

注意,有两个jdk可用。如果您没有注意到需要切换到的Java版本,请从https://www.oracle.com/java/technologies/downloads/下载并安装相应的版本 (JDK 8表示为1.8)。 安装了适当的JDK之后,重复此步骤。

注意要切换到的JDK版本。例如,“11.0”和“1.8”是上面示例中可用的JDK版本。 切换到所需的版本。例如,如果您希望切换到JDK 8,则输入如下一行: export JAVA_HOME=/usr/libexec/ JAVA_HOME

对于11.0,将“1.8”转换为“11.0” 4. 输入Terminal检查JDK版本:

java -version

如果正确地执行了所有步骤,那么JDK版本应该与您在最后一步中指定的版本相关联。 5. (可选)要使其成为JDK的默认版本,在Terminal中输入以下内容:

open ~/.bash_profile

然后,将步骤3中的终端输入添加到这个文件中:

切换到Java版本8

export JAVA_HOME=`/usr/libexec/java_home -v 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.

我最近回复,我真的建议你用SDKMAN代替Homebrew。

使用SDKMAN,您可以轻松地在mac上安装不同版本的JAVA,并从一个版本切换到另一个版本。

你也可以使用SDKMAN的ANT, GRADLE, KOTLIN, MAVEN, SCALA等…

要在mac上安装新版本,可以运行命令sdk install java 15.0.0.j9-adpt