homebrew 安装 OpenJDK

homebrew 安装 OpenJDK

使用 homebrew-openjdk 安装 AdoptOpenJDK

安装 JDK

shell
1
2
brew tap AdoptOpenJDK/openjdk
brew cask install adoptopenjdk11

在国内由于 github 访问速度,可能会出现安装(下载)失败的情况:

log
1
2
curl: (18) transfer closed with 100324834 bytes remaining to read
Error: Download failed on Cask 'adoptopenjdk11' with message: Download failed: https://github.com/AdoptOpenJDK/openjdk11-binaries/releases/download/jdk-11.0.8%2B10/OpenJDK11U-jdk_x64_mac_hotspot_11.0.8_10.pkg

解决方法:

加速 OpenJDK 下载

使用清华镜像站加速获取 pkg 包

修改 /usr/local/Homebrew/Library/Taps/adoptopenjdk/homebrew-openjdk/Casks/adoptopenjdk11.rb 中 url 地址为国内源

示例:

adoptopenjdk11.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# PLEASE DO NOT EDIT THIS FILE. EDIT Templates/adoptopenjdk.rb.tmpl
cask "adoptopenjdk11" do
version "11.0.8,10"
sha256 "c9ce3e78a7ec7f8c23041af63a66bfe18bbf75c7bfa8f8a51148a098f3653699"

# github.com/AdoptOpenJDK was verified as official when first introduced to the cask
# url "https://github.com/AdoptOpenJDK/openjdk11-binaries/releases/download/jdk-11.0.8%2B10/OpenJDK11U-jdk_x64_mac_hotspot_11.0.8_10.pkg"
# 使用清华镜像站加速
url "https://mirrors.tuna.tsinghua.edu.cn/AdoptOpenJDK/11/jdk/x64/mac/OpenJDK11U-jdk_x64_mac_hotspot_11.0.8_10.pkg"
appcast "https://github.com/AdoptOpenJDK/openjdk#{version.major}-binaries/releases/latest"
name "AdoptOpenJDK 11"
homepage "https://adoptopenjdk.net/"

pkg "OpenJDK11U-jdk_x64_mac_hotspot_11.0.8_10.pkg"

postflight do
system_command "/usr/sbin/pkgutil", args: ["--pkg-info", "net.adoptopenjdk.11.jdk"], print_stdout: true
end

uninstall pkgutil: "net.adoptopenjdk.11.jdk"
end

保存后再次执行命令安装即可

shell
1
brew cask install adoptopenjdk11

多版本 JDK 切换

  1. 按照 homebrew-openjdk 文档中描述,在 ~/.bashrc~/.zshrc 中添加如下内容:

    ~/.zshrc
    1
    2
    3
    4
    5
    jdk() {
    version=$1
    export JAVA_HOME=$(/usr/libexec/java_home -v"$version");
    java -version
    }
  2. 加载配置

    shell
    1
    source ~/.zshrc
  3. 选择已安装的 JDK 版本

1
2
3
4
# 选择 OpenJDK 1.8
jdk 1.8
# 选择 OpenJDK 11
jdk 11
评论

:D 一言句子获取中...

加载中,最新评论有1分钟缓存...