首頁 > Java > java教程 > 主體

在 jEnv 管理的 MacOS 上安裝(多個)Java

Patricia Arquette
發布: 2024-10-07 16:07:29
原創
872 人瀏覽過

Installing (multiple) Java on MacOS managed by jEnv

Install some Java!

I'm gonna install three version's from openjdk using Homebrew! We should all know what Homebrew is by now...
$ brew install openjdk@17
$ brew install openjdk@21

Create some Symlinks

As far as I understand it MacOS has a special Java VM folder where it installs the JVMs. We'll create symlinks to versions we just installed from Homebrew.
sudo ln -sfn /opt/homebrew/opt/openjdk@17/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-17.jdk
sudo ln -sfn /opt/homebrew/opt/openjdk@21/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-21.jdk

Install jEnv

jenv is a Java installation version manager. Install with Homebrew as well:
$ brew install jenv

Add the following to your shell's .rc file so jenv is executed upon terminal startup


# Setup JEnv to install run
export PATH="$HOME/.jenv/bin:$PATH"
eval "$(jenv init -)"
# Have JAVA_HOME set by JEnv for us...
jenv enable-plugin export


登入後複製

Source the file to apply the changes
source ~/.zshrc # or ~/.bash_profile, ~/.bashrc, etc.

Add the JVMs to jenv

Add the versions in Java VM folder to jenv so it can manage them
jenv add /Library/Java/JavaVirtualMachines/openjdk-17.jdk/Contents/Home/
jenv add /Library/Java/JavaVirtualMachines/openjdk-21.jdk/Contents/Home/

See the versions installed by jenv

You can see the installed versions by echo ${JAVA_HOME}


$ jenv versions
* system (set by /Users/user/.jenv/version)
  17
  17.0
  17.0.12
  21
  21.0
  21.0.4
  openjdk64-17.0.12
  openjdk64-21.0.4


登入後複製

See which version of Java you're using:


$ java -version
openjdk version "17.0.12" 2024-07-16
OpenJDK Runtime Environment Homebrew (build 17.0.12+0)
OpenJDK 64-Bit Server VM Homebrew (build 17.0.12+0, mixed mode, sharing)


登入後複製

Set/unset global version of Java


$ jenv global 21.0.2
$ jenv global --unset


登入後複製

Set/unset local version of Java

This will add a .java-version file in the directory you're currently in so you can commit it as a part of your Java project.


$ jenv local 21.0.2
$ jenv local --unset


登入後複製

Congrats! You've just installed Java on your MacOS! Have fun on your Java/Kotlin projects! ?

以上是在 jEnv 管理的 MacOS 上安裝(多個)Java的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:dev.to
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!