如何在macOS上安装jdk插图

本文介绍如何在macOS上安装java jdk、自制包管理器、手动安装以及在不同JDK版本之间切换。

测试环境:

  • macOS 11 Big Sur
  • Homebrew 2.7.4
  • JDK 8、14、16、16(采用OpenJDK和OpenJDK)

注意

从MacOS 10.15 Catalina开始,默认的终端shell从bash(BourneShell)切换到zsh(Zshell)。我们应该将~/.bash_profile~/.bashrc中的所有启动脚本和环境变量移到~/.zshenv~/.zshrc

在macOS上安装最新的Java

1. 安装并升级Homebrew。

2. brew search java以查找所有可用的java相关公式。

% brew search java      

==> Formulae
app-engine-java           java                      javacc                    jslint4java               pdftk-java
google-java-format        java11                    javarepl                  libreadline-java

3. brew info 以显示公式详细信息。

java总是包含最新的java jdk(OpenJDK)GA版本;在编写本文时,最新的GA是jdk15。

% brew info java

openjdk: stable 15.0.1 (bottled) [keg-only]
Development kit for the Java programming language
http://openjdk.java.net/
/usr/local/Cellar/openjdk/15.0.1 (614 files, 324.9MB)
  Poured from bottle on 2020-12-09 at 09:06:07
From: http://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/openjdk.rb
License: Cannot Represent

java11 formula 包含java11lts版本。

% brew info java11  

openjdk@11: stable 11.0.9 (bottled) [keg-only]
Development kit for the Java programming language
http://openjdk.java.net/
Not installed
From: http://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/openjdk@11.rb
License: GPL-2.0-only

4. brew install java以安装最新的JDK 15。

% brew info java

5. Homebrew在哪里安装java?

Homebrew在/usr/local/Cellar/openjdk/安装了JDK文件和目录,/usr/local/opt/openjdk的符号链接指向最新的Java15.0.1版本。

% ls -lsa /usr/local/Cellar/openjdk/      

total 0
0 drwxr-xr-x   3 mkyong  staff    96 Dec  9 09:06 .
0 drwxrwxr-x  69 mkyong  admin  2208 Jan 15 15:35 ..
0 drwxr-xr-x   9 mkyong  staff   288 Jan 15 16:47 15.0.1

% ls -lsa /usr/local/opt/openjdk
0 lrwxr-xr-x  1 mkyong  admin  24 Dec  9 09:06 /usr/local/opt/openjdk -> ../Cellar/openjdk/15.0.1

6. java formula是keg only,这意味着它安装在/usr/local/Cellar中,但没有链接到/usr/local/bin/Library/java/JavaVirtualMachines/(macOS/usr/bin/java wrapper)等位置。

为了让macOS /usr/bin/java wrapper找到已安装的JDK,我们在/Library/java/JavaVirtualMachines/处手动创建一个符号链接。

% sudo ln -sfn /usr/local/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk

7. 完成

% java -version

openjdk version "15.0.1" 2020-10-20
OpenJDK Runtime Environment (build 15.0.1+9)
OpenJDK 64-Bit Server VM (build 15.0.1+9, mixed mode, sharing)

在macOS上安装Java8

1. 安装和升级Homebrew。

2. brew search java有没有java8

% brew search java        
==> Formulae
app-engine-java           java                      javacc                    jslint4java               pdftk-java
google-java-format        java11                    javarepl                  libreadline-java

3. Java8在openjdk@8 formula。openjdk与java formula相同,并且它总是包含最新的JDK GA版本openjdk@11与java11 formula相同,包含JDK 11。

% brew search openjdk
==> Formulae
openjdk                                   openjdk@11                                  openjdk@8  

4. 我们可以使用openjdk@8 formula在macOS上安装Java8。

% brew install openjdk@8

5. 这个openjdk@8也只是一个keg-only;我们需要创建一个符号链接,以便macos java包装器可以找到它。

% sudo ln -sfn /usr/local/opt/openjdk@8/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-8.jdk  

6. 完成

% java -version

openjdk version "1.8.0_275"
OpenJDK Runtime Environment (build 1.8.0_275-bre_2020_11_16_15_09-b00)
OpenJDK 64-Bit Server VM (build 25.275-b00, mixed mode)

7. 如果我们安装了多个Java版本呢?

ls -lsah /Library/Java/JavaVirtualMachines/               

openjdk-8.jdk -> /usr/local/opt/openjdk@8/libexec/openjdk.jdk
openjdk.jdk -> /usr/local/opt/openjdk/libexec/openjdk.jdk

我们可以在~/.zshrc处更新$PATH,这样macOS就可以找到正确安装的Java。

% java -version

openjdk version "15.0.1" 2020-10-20
OpenJDK Runtime Environment (build 15.0.1+9)
OpenJDK 64-Bit Server VM (build 15.0.1+9, mixed mode, sharing)

% echo 'export PATH="/usr/local/opt/openjdk@8/bin:$PATH"' >> ~/.zshrc  

% source ~/.zshrc

% java -version

openjdk version "1.8.0_275"
OpenJDK Runtime Environment (build 1.8.0_275-bre_2020_11_16_15_09-b00)
OpenJDK 64-Bit Server VM (build 25.275-b00, mixed mode)

在macOS上设置$JAVA_HOME环境变量请参考这篇文章:https://javakk.com/1559.html

自制在macOS上安装指定的Java(OpenJDK)

adoptopenjdk/openjdk tap包含许多不同的JDK(adoptopenjdk)版本。

1. 通过openjdk/openjdk添加新的tap(第三方存储库)。

% brew tap adoptopenjdk/openjdk

2. 搜索相关的OpenJDK formula。

% brew search adoptopenjdk

==> Casks
adoptopenjdk                              adoptopenjdk12-openj9                     adoptopenjdk14-openj9-large
adoptopenjdk-jre                          adoptopenjdk12-openj9-jre                 adoptopenjdk15
adoptopenjdk-openj9                       adoptopenjdk12-openj9-jre-large           adoptopenjdk15-jre
adoptopenjdk-openj9-jre                   adoptopenjdk12-openj9-large               adoptopenjdk15-openj9
adoptopenjdk-openj9-jre-large             adoptopenjdk13                            adoptopenjdk15-openj9-jre
adoptopenjdk-openj9-large                 adoptopenjdk13-jre                        adoptopenjdk15-openj9-jre-large
adoptopenjdk10                            adoptopenjdk13-openj9                     adoptopenjdk15-openj9-large
adoptopenjdk11                            adoptopenjdk13-openj9-jre                 adoptopenjdk8
adoptopenjdk11-jre                        adoptopenjdk13-openj9-jre-large           adoptopenjdk8
adoptopenjdk11-openj9                     adoptopenjdk13-openj9-large               adoptopenjdk8-jre
adoptopenjdk11-openj9-jre                 adoptopenjdk14                            adoptopenjdk8-openj9
adoptopenjdk11-openj9-jre-large           adoptopenjdk14-jre                        adoptopenjdk8-openj9-jre
adoptopenjdk11-openj9-large               adoptopenjdk14-openj9                     adoptopenjdk8-openj9-jre-large
adoptopenjdk12                            adoptopenjdk14-openj9-jre                 adoptopenjdk8-openj9-large
adoptopenjdk12-jre                        adoptopenjdk14-openj9-jre-large           adoptopenjdk9

3. 我们选择采用openjdk14 formula来安装JDK14。

% brew install adoptopenjdk14

installer: Package name is AdoptOpenJDK
installer: Installing at base path /
installer: The install was successful.
package-id: net.adoptopenjdk.14.jdk
version: 14.0.2+12
volume: /
location: Library/Java/JavaVirtualMachines/adoptopenjdk-14.jdk
install-time: 1610720586
adoptopenjdk14 was successfully installed!

4. adoptopenjdk formula直接在/Library/Java/JavaVirtualMachines/中创建JDK home。

% ls -lsa /Library/Java/JavaVirtualMachines/

adoptopenjdk-14.jdk
openjdk-8.jdk -> /usr/local/opt/openjdk@8/libexec/openjdk.jdk
openjdk.jdk -> /usr/local/opt/openjdk/libexec/openjdk.jdk

5. 由于JDK home已经在/Library/Java/JavaVirtualMachines/中,我们不需要创建任何符号链接,只需导出$PATH并将其指向正确的openjdk-14.JDK。

% echo 'export PATH="/Library/Java/JavaVirtualMachines/adoptopenjdk-14.jdk/Contents/Home/bin:$PATH"' >> ~/.zshrc

% source ~/.zshrc

% java -version
openjdk version "14.0.2" 2020-07-14
OpenJDK Runtime Environment AdoptOpenJDK (build 14.0.2+12)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 14.0.2+12, mixed mode, sharing)

在macOS上手动安装Java(早期访问版本)

出于某些原因,我们需要在macOS上手动安装Java:

  • Anti-Homebrew,开发者喜欢控制一切。
  • 与早期的access版本或oraclejdk一样,JDK版本不存在于自制存储库中。

1. 访问JDK 16早期访问构建网站并下载JDK。

如何在macOS上安装jdk插图1

2. 提取下载的tar.gz文件到/Library/Java/JavaVirtualMachines

% sudo tar xvzf ~/Downloads/openjdk-16-ea+32_osx-x64_bin.tar.gz -C /Library/Java/JavaVirtualMachines

% ls -lsa /Library/Java/JavaVirtualMachines

adoptopenjdk-14.jdk
jdk-16.jdk
openjdk-8.jdk -> /usr/local/opt/openjdk@8/libexec/openjdk.jdk
openjdk.jdk -> /usr/local/opt/openjdk/libexec/openjdk.jdk

3. 更新$PATH并指向/Library/Java/JavaVirtualMachines/jdk-16.jdk

% echo 'export PATH="/Library/Java/JavaVirtualMachines/jdk-16.jdk/Contents/Home/bin:$PATH"' >> ~/.zshrc

4. 测试JDK16

% source ~/.zshrc

% java -version
openjdk version "16-ea" 2021-03-16
OpenJDK Runtime Environment (build 16-ea+32-2190)
OpenJDK 64-Bit Server VM (build 16-ea+32-2190, mixed mode, sharing)

在不同的JDK版本之间切换

从上面的示例1到示例4,我们在macOS上安装了四个JDK版本。

1. 列出macOS上的所有JDK版本。

% /usr/libexec/java_home -V

Matching Java Virtual Machines (4):
  16 (x86_64) "Oracle Corporation" - "OpenJDK 16-ea" /Library/Java/JavaVirtualMachines/jdk-16.jdk/Contents/Home
  15.0.1 (x86_64) "UNDEFINED" - "OpenJDK 15.0.1" /usr/local/Cellar/openjdk/15.0.1/libexec/openjdk.jdk/Contents/Home
  14.0.2 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 14" /Library/Java/JavaVirtualMachines/adoptopenjdk-14.jdk/Contents/Home
  1.8.0_275 (x86_64) "UNDEFINED" - "OpenJDK 8" /usr/local/Cellar/openjdk@8/1.8.0+275/libexec/openjdk.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/jdk-16.jdk/Contents/Home

% ls -lsa /Library/Java/JavaVirtualMachines

adoptopenjdk-14.jdk
jdk-16.jdk
openjdk-8.jdk -> /usr/local/opt/openjdk@8/libexec/openjdk.jdk
openjdk.jdk -> /usr/local/opt/openjdk/libexec/openjdk.jdk

2. 在~/.zshrc(适用于macOS 10.15 Catalina及更高版本)或~/.bashrc(适用于macOS 10.15 Catalina之前)中添加以下函数。

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

如果文件~/.zshrc不存在,请创建它。

3. 来源~/.zshrc以反映变化。

% source ~/.zshrc

4. 在不同的JDK版本之间切换。

% java -version
openjdk version "16-ea" 2021-03-16
OpenJDK Runtime Environment (build 16-ea+32-2190)
OpenJDK 64-Bit Server VM (build 16-ea+32-2190, mixed mode, sharing)

% jdk 1.8
openjdk version "1.8.0_275"
OpenJDK Runtime Environment (build 1.8.0_275-bre_2020_11_16_15_09-b00)
OpenJDK 64-Bit Server VM (build 25.275-b00, mixed mode)

% jdk 14
openjdk version "14.0.2" 2020-07-14
OpenJDK Runtime Environment AdoptOpenJDK (build 14.0.2+12)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 14.0.2+12, mixed mode, sharing)

% jdk 15
openjdk version "15.0.1" 2020-10-20
OpenJDK Runtime Environment (build 15.0.1+9)
OpenJDK 64-Bit Server VM (build 15.0.1+9, mixed mode, sharing)

% jdk 16
openjdk version "16-ea" 2021-03-16
OpenJDK Runtime Environment (build 16-ea+32-2190)
OpenJDK 64-Bit Server VM (build 16-ea+32-2190, mixed mode, sharing)