Install Multiple Java Versions on Mac by Homebrew Cask

多个Java版本切换和安装 jenv

Posted by Yuankun Li on 2021-06-06

安装Homebrew Cask

brew install 和 brew cask install 的区别

homebrew 官网

Install Homebrew Cask first if you haven’t:

1
2
3
4
5
6
7
8
brew update
# 老版本 brew tap caskroom/cask
# 会报错:
# Error: caskroom/cask was moved. Tap homebrew/cask instead.
# 2021 新版本:
brew tap homebrew/cask
# 老版本: brew install brew-cask
brew install homebrew/cask

安装brew-cask-completion:

1
brew install brew-cask-completion

But before doing that, let’s check if we already have JDK 7 installed by Homebrew Cask:

1
2
3
4
5
# 老版本 brew tap caskroom/versions
# Error: caskroom/versions was moved. Tap homebrew/cask-versions instead.
brew tap homebrew/cask-versions
# 老版本:brew cask info java7
brew info --cask java7

How to Use Homebrew Cask

安装Java

安装最新版本Java

Otherwise, install Latest Version of Java Using Brew:

1
2
3
4
# 老版本 brew cask install java
# 会报错:
# Error: Unknown command: cask
brew install --cask java

安装指定版本Java

[New]安装openjdk11

Homebrew 安装openjdk11
MACOS 升级M1芯片后,最好更新下jdk版本,运行效率更高。
另外因为CVE-2022-21476 vulnerability resolution (aka Java upgrade to jdk-11.0.15+10) ,openjdk11最好更新到11.0.15+10.

  1. Install
1
2
3
4
5
6
7
8
9
10
11
12
13
$ brew install openjdk@11

For the system Java wrappers to find this JDK, symlink it with
sudo ln -sfn /opt/homebrew/opt/openjdk@11/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-11.jdk

openjdk@11 is keg-only, which means it was not symlinked into /opt/homebrew,
because this is an alternate version of another formula.

If you need to have openjdk@11 first in your PATH, run:
echo 'export PATH="/opt/homebrew/opt/openjdk@11/bin:$PATH"' >> ~/.zshrc

For compilers to find openjdk@11 you may need to set:
export CPPFLAGS="-I/opt/homebrew/opt/openjdk@11/include"
  1. Set jdk for jenv
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$ where java
/opt/homebrew/opt/openjdk@11/bin/java
/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home

$ jenv versions
openjdk64-11.0.11

$ jenv add /opt/homebrew/opt/openjdk@11

$ jenv versions
* openjdk64-11.0.11 (set by /Users/yuanli/.jenv/version)
openjdk64-11.0.15

# set default jdk version
$ jenv global openjdk64-11.0.15

$ jenv versions
openjdk64-11.0.11
* openjdk64-11.0.15 (set by /Users/yuanli/.jenv/version)
  1. (Optional)删除旧的jdk
1
2
3
4
5
6
$ sudo rm -rf /Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home/bin/java

$ jenv remove openjdk64-11.0.11

$ jenv versions
openjdk64-11.0.15
  1. Set jdk for IDE

ERROR in terminal

~/.bash_profile 或是~/.zprofile ~/.zshrc里面配置了JAVA_HOME

1
export JAVA_HOME="/opt/homebrew/opt/openjdk@11"
  • 报错:
1
ERROR: JAVA_HOME is set to an invalid directory: /opt/homebrew/opt/openjdk@11
  1. JAVA_HOME 修改为: export JAVA_HOME="$(/usr/libexec/java_home)"
  2. Run sudo ln -sfn /usr/local/opt/openjdk/libexec/openjdk.jdk

other

Install Specific Versions of Java (Java8, Java11, Java13)
To install previous or specific versions of JDKs, you can get them from AdoptOpenJDK:

1
2
3
4
5
brew tap adoptopenjdk/openjdk

brew install --cask adoptopenjdk8
brew install --cask adoptopenjdk11
brew install --cask adoptopenjdk13

Java版本切换

Now it is time to install jEnv:

1
brew install jenv

Add the following lines to ~/.bash_profile. This will initialize jEnv.

1
2
# Init jenv
if which jenv > /dev/null; then eval "$(jenv init -)"; fi

jEnv doesn’t install JDKs, so we have to tell jEnv where to look for them. Type these commands to register JDKs in jEnv (replace the minor and patch versions with yours):

1
2
3
4
$ where java
/usr/bin/java
/Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home/bin/java
/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home/bin/java
1
2
3
4
# 添加jdk变量到jenv,只有添加过jenv versions 才能显示出来
$ jenv add /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
$ jenv add /Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home
$ jenv add /usr

After that, run this command to list all registered JDKs:

1
2
3
4
$ jenv versions

openjdk64-1.8.0.292
openjdk64-11.0.11
1
2
# 移除jdk变量
jenv remove openjdk64-11.0.11

The version with an asterisk is the active version.
In my case, I need to keep JDK 7 as my default version, so I set the global version to 1.7:

1
jenv global oracle64-1.7.0.79

And in my project, I set the local JDK version to 1.8:

1
2
cd <my project>
jenv local oracle64-1.8.0.66

The above command will create a .java-version file at project root. Its content is the version I just picked for this project:

1
oracle64-1.8.0.66

jenv enable-plugin export

用 jenv 管理 JDK 版本的小伙伴,可以通过这个命令让 jenv 在切版本的时候顺便把 JAVA_HOME 设置正确,运行一次即可。
jenv enable-plugin export

Reference



show git comment