網頁

2024/3/25

Mac 安裝java openjdk@21 by Homebrew

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


環境:

  • MacBook Pro Apple M1
  • Ventura 13.0.1
  • Zsh


安裝openjdk@21

在終端機輸入brew install openjdk@21開始安裝。

% brew install openjdk@21
...
==> Installing openjdk
==> Pouring openjdk--21.0.2.arm64_ventura.bottle.tar.gz
==> Caveats
For the system Java wrappers to find this JDK, symlink it with
  sudo ln -sfn /opt/homebrew/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk

openjdk is keg-only, which means it was not symlinked into /opt/homebrew,
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="/opt/homebrew/opt/openjdk/bin:$PATH"' >> ~/.zshrc

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


建立Symlink

然後輸入sudo ln -sfn /opt/homebrew/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk設定macOS預設的JDK為Homebrew安裝的OpenJDK。

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

設定Path變數

輸入echo 'export PATH="/opt/homebrew/opt/openjdk/bin:$PATH"' >> ~/.zshrc將OpenJDK的bin目錄設為Path變數到.zshrc

% echo 'export PATH="/opt/homebrew/opt/openjdk/bin:$PATH"' >> ~/.zshrc

輸入source ~/.zshrc重新載入.zshrc

% source ~/.zshrc

輸入java --version顯示版本資訊代表安裝成功。

% java --version
openjdk 21.0.2 2024-01-16
OpenJDK Runtime Environment Homebrew (build 21.0.2)
OpenJDK 64-Bit Server VM Homebrew (build 21.0.2, mixed mode, sharing)


沒有留言:

張貼留言