網頁

2022/3/28

Mac 安裝java openjdk@17 by Homebrew

Mac使用Homebrew安裝Java JDK 17 (Formulea openjdk@17)的方式如下。


範例環境:

  • macOS BigSur version 10.15.5
  • Homebrew 3.4.4


移除舊的JDK

若之前沒用Homebrew安裝過可省略此步驟。

上一次是在2020年裝的brew cask java 11

~$ java -version
openjdk 11.0.2 2019-01-15
OpenJDK Runtime Environment 18.9 (build 11.0.2+9)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.2+9, mixed mode)

先把舊的cask java11移除,輸入brew uninstall java11

~$ brew uninstall java11
==> Uninstalling Cask java11
==> Backing Generic Artifact 'openjdk-11.0.2.jdk' up to '/usr/local/Caskroom/jav
Password:
==> Removing Generic Artifact '/Library/Java/JavaVirtualMachines/openjdk-11.0.2.
==> Purging files for version 11.0.2,9 of Cask java11


安裝openjdk@17

執行brew install openjdk@17開始安裝。

$ brew install openjdk@17
...
==> Downloading https://ghcr.io/v2/homebrew/core/openjdk/manifests/17.0.2
Already downloaded: /Users/matt/Library/Caches/Homebrew/downloads/bf7b135c802dd11fdadc2b477da9226d11d9f7db5bcfe5760c32f65e979a630b--openjdk-17.0.2.bottle_manifest.json
==> Downloading https://ghcr.io/v2/homebrew/core/openjdk/blobs/sha256:2376255643
Already downloaded: /Users/matt/Library/Caches/Homebrew/downloads/b25a6416c79c2a96d2f219c8f07e1967a1921766d3145d054b769c8d7f274bc3--openjdk--17.0.2.big_sur.bottle.tar.gz
==> Pouring openjdk--17.0.2.big_sur.bottle.tar.gz
==> Caveats
For the system Java wrappers to find this JDK, symlink it with
  sudo ln -sfn /usr/local/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk

openjdk is keg-only, which means it was not symlinked into /usr/local,
because macOS provides similar software and installing this software in
parallel can cause all kinds of trouble.

If you need to have openjdk first in your PATH, run:
  echo 'export PATH="/usr/local/opt/openjdk/bin:$PATH"' >> /Users/matt/.bash_profile

For compilers to find openjdk you may need to set:
  export CPPFLAGS="-I/usr/local/opt/openjdk/include"

==> Summary
🍺  /usr/local/Cellar/openjdk/17.0.2: 639 files, 305.4MB
==> Running `brew cleanup openjdk`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).

執行export PATH="/usr/local/opt/openjdk/bin:$PATH"' >> /Users/<user>/.bash_profile設定PATH變數<user>為Mac使用者名稱。

~$ echo 'export PATH="/usr/local/opt/openjdk/bin:$PATH"' >> /Users/<user>/.bash_profile

從訊息可得知Homebrew Formulae openjdk的安裝路徑在/usr/local/opt/openjdk


重新開啟terminal,輸入java -version檢視目前的Java版本更新為openjdk 17.0.2。

~$ java -version
openjdk version "17.0.2" 2022-01-18
OpenJDK Runtime Environment Homebrew (build 17.0.2+0)
OpenJDK 64-Bit Server VM Homebrew (build 17.0.2+0, mixed mode, sharing)


沒有留言:

張貼留言