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


当前回答

为了安装最新版本的OpenJDK,我使用了这个。OpenJDK 14的示例:

brew info adoptopenjdk
brew tap adoptopenjdk/openjdk
brew cask install adoptopenjdk14

查看https://github.com/AdoptOpenJDK/homebrew-openjdk获取当前信息。

其他回答

下面是一个更DRY的bash版本(基于Vegard的回答)

将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

为了安装最新版本的OpenJDK,我使用了这个。OpenJDK 14的示例:

brew info adoptopenjdk
brew tap adoptopenjdk/openjdk
brew cask install adoptopenjdk14

查看https://github.com/AdoptOpenJDK/homebrew-openjdk获取当前信息。

我使用的是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.

在这个网站上可以找到 让我们从安装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: JDK 7: JDK 8: 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 java -version

就是这样。我们现在有多个版本的java,我们可以很容易地在它们之间切换。jEnv还有其他一些特性,比如Gradle、Ant、Maven等的包装器,以及全局或本地设置JVM选项的能力。有关更多信息,请查看文档。

使用jenv管理MAC中的多个java版本

Install homebrew using following command /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)" install jenv and activate jenv brew install jenv echo 'eval "$(jenv init -)"' >> ~/.bash_profile tap cask-versions brew tap homebrew/cask-versions search available java version that can be installed brew search java E.g. to install java6 use following command brew install cask java6 Add multiple versions of java in jenv jenv add /Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home jenv add /Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

注意:-如果你得到类似ln: /Users/<username>/.jenv/versions/oracle64-1.8.0.231: No such file or directory的错误,那么执行以下命令:-

   mkdir -p /Users/<username>/.jenv/versions/oracle64-1.8.0.231

在添加jdk 's后重新哈希jenv jenv重复 列出已知的java版本到jenv jenv版本 设置默认版本 Jenv global oracle64-1.8.0.231 更改项目的java版本 Jenv local oracle64-1.6.0.65 将JAVA_HOME设置为与jenv相同的版本 Jenv执行bash echo $ JAVA_HOME