我想在Mac OSX上安装OpenJDK Java,并让它与其他JDK一起工作,因为它是一个更新的版本。目前,我下载了tar.gz并将其放在我的路径中,但这很难维护。

我发现唯一一个能自动做更多事情的安装是通过Homebrew桶进行安装。它看起来也只有当前版本:

brew cask info java

显示:

java: 13,33:5b8a42f3905b406298b72d750b6919f6 https://openjdk.java.net/

所以我可以从那里安装,然后呢?我是否只能使用新版本?


当前回答

要保持特定的主要版本,请使用brew tap激活AdoptOpenJDK tap,然后使用brew cask install安装所需的版本:

$ brew tap AdoptOpenJDK/openjdk
$ brew cask install <version>

要安装带有HotSpot的AdoptOpenJDK 14,运行:

$ brew tap AdoptOpenJDK/openjdk
$ brew cask install adoptopenjdk14

其他回答

恕我直言,没有必要安装所有额外的应用程序/包。

使用命令检查可用的版本:

> /usr/libexec/java_home -V
Matching Java Virtual Machines (8):
    11, x86_64: "Java SE 11-ea" /Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home
    10.0.2, x86_64: "Java SE 10.0.2"    /Library/Java/JavaVirtualMachines/jdk-10.0.2.jdk/Contents/Home
    9.0.1, x86_64:  "Java SE 9.0.1" /Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home
    1.8.0_181-zulu-8.31.0.1, x86_64:    "Zulu 8"    /Library/Java/JavaVirtualMachines/zulu-8.jdk/Contents/Home
    1.8.0_151, x86_64:  "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home
    1.7.0_80, x86_64:   "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home
    1.6.0_65-b14-468, x86_64:   "Java SE 6" /Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
    1.6.0_65-b14-468, i386: "Java SE 6" /Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

现在,如果你想在上面的列表中选择Azul JDK 8,而不是Oracle的Java SE 8,调用如下命令:

> /usr/libexec/java_home -v 1.8.0_181
/Library/Java/JavaVirtualMachines/zulu-8.jdk/Contents/Home

要选择Oracle的Java SE 8,您将调用以下命令:

> /usr/libexec/java_home -v 1.8.0_151
/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home

正如你所看到的,提供的版本号应该是唯一的字符串集:1.8.0_181 vs 1.8.0_151

您可以使用asdf在多个java版本之间进行安装和切换。它也有其他语言的插件。您可以使用Homebrew安装asdf

brew install asdf

配置asdf时,需要安装java plugin

asdf plugin-add java

选择要安装的版本

asdf list-all java

例如,安装和配置adoptopenjdk8

asdf install java adoptopenjdk-8.0.272+10
asdf global java adoptopenjdk-8.0.272+10

最后,如果需要,为shell配置JAVA_HOME。只需添加到shell初始化脚本,例如~/。zsh时的ZSHRC:

. ~/.asdf/plugins/java/set-java-home.zsh

我是这么做的。

步骤1:安装Java 11

你可以从这里下载Java 11 dmg for mac: https://www.oracle.com/technetwork/java/javase/downloads/jdk11-downloads-5066655.html

步骤2:安装Java 11后。确认所有版本的安装。在终端中输入以下命令。

/usr/libexec/java_home -V

步骤3:编辑.bash_profile

sudo nano ~/.bash_profile

步骤4:添加11.0.1作为默认值。(将以下行添加到bash_profile文件中)。

export JAVA_HOME=$(/usr/libexec/java_home -v 11.0.1)

切换到任何版本

export JAVA_HOME=$(/usr/libexec/java_home -v X.X.X)

现在按CTRL+X退出bash。按“Y”保存更改。

步骤5:重新加载bash_profile

source ~/.bash_profile

步骤6:确认当前的Java版本

java -version

要保持特定的主要版本,请使用brew tap激活AdoptOpenJDK tap,然后使用brew cask install安装所需的版本:

$ brew tap AdoptOpenJDK/openjdk
$ brew cask install <version>

要安装带有HotSpot的AdoptOpenJDK 14,运行:

$ brew tap AdoptOpenJDK/openjdk
$ brew cask install adoptopenjdk14

这个答案扩展了Jayson的优秀答案,并对您的用例的最佳方法提供了一些更有见解的指导:

SDKMAN is the best solution for most users. It's easy to use, doesn't have any weird configuration, and makes managing multiple versions for lots of other Java ecosystem projects easy as well. Downloading Java versions via Homebrew and switching versions via jenv is a good option, but requires more work. For example, the Homebrew commands in this highly upvoted answer don't work anymore. jenv is slightly harder to setup, the plugins aren't well documented, and the README says the project is looking for a new maintainer. jenv is still a great project, solves the job, and the community should be thankful for the wonderful contribution. SDKMAN is just the better option cause it's so great. Jabba is written is a multi-platform solution that provides the same interface on Mac, Windows, and PC (it's written in Go and that's what allows it to be multiplatform). If you care about a multiplatform solution, this is a huge selling point. If you only care about running multiple versions on your Mac, then you don't need a multiplatform solution. SDKMAN's support for tens of popular SDKs is what you're missing out on if you go with Jabba.

手动管理版本可能是最糟糕的选择。如果你决定手动切换版本,你可以使用这个Bash代码,而不是Jayson的冗长代码(代码片段来自homebrew-openjdk README:

jdk() {
        version=$1
        export JAVA_HOME=$(/usr/libexec/java_home -v"$version");
        java -version
 }

Jayson的回答提供了SDKMAN和jenv的基本命令。如果你想了解更多关于这些工具的背景知识,这里有更多关于SDKMAN和jenv的信息。