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

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

brew cask info java

显示:

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

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


当前回答

另一种选择是使用SDKMAN!参见https://wimdeblauwe.wordpress.com/2018/09/26/switching-between-jdk-8-and-11-using-sdkman/

首先安装SDKMAN: https://sdkman.io/install然后…

安装Oracle JDK 8使用 安装OpenJDK 11

开关:

使用java 8.0.181-oracle切换到带有sdk的JDK 8 使用java 11.0.0-open切换到带有sdk的JDK 11

设置默认值:

默认JDK 8, sdk默认java 8.0.181-oracle 默认为JDK 11, sdk默认为java 11.0.0-open

其他回答

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

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

> /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

要保持特定的主要版本,请使用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

您可以以任何您想要的方式安装JDK版本。

家酿 SDK的男人 手动

然后,我建议使用JENV在不同版本之间切换,并在全局或本地使用所需的JDK。

你可以在这里找到更多关于如何安装和使用JENV的细节-> https://blog.adamgamboa.dev/2021/06/17/using-jenv-to-switch-jkd-versions/

您可以使用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

这个答案扩展了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的信息。