Homebrew 安装 Java8
brew tap adoptopenjdk/openjdk
brew cask install adoptopenjdk8
如果提示两个 tap
冲突,使用 untap
命令:
brew untap ...
JAVA_HOME
编辑 .bash_profile
,添加 JAVA_HOME
:
# JDK 8
export JAVA_HOME_8=/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
export JAVA_HOME_13=/Library/Java/JavaVirtualMachines/openjdk-13.0.1.jdk/Contents/Home
export PATH=$PATH:$JAVA_HOME_8
alias jdk8="export JAVA_HOME=$JAVA_HOME_8"
alias jdk13="export JAVA_HOME=$JAVA_HOME_13"
刷新配置:
source ~/.bash_profile
或者如果是 zsh
source ~/.zshrc
切换版本
➜ ~ jdk8
➜ ~ java -version
openjdk version "1.8.0_242"
OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_242-b08)
OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.242-b08, mixed mode)
➜ ~ jdk13
➜ ~ java -version
openjdk version "13.0.1" 2019-10-15
OpenJDK Runtime Environment (build 13.0.1+9)
OpenJDK 64-Bit Server VM (build 13.0.1+9, mixed mode, sharing)
默认选择低版本Java
Solution without 3rd party tools:
leave all JDKs at their default location, under /Library/Java/JavaVirtualMachines. The system will pick the highest version by default.
To exclude a JDK from being picked by default, rename its Contents/Info.plist to Info.plist.disabled. That JDK can still be used when $JAVA_HOME points to it, or explicitly referenced in a script or configuration. It will simply be ignored by system's java command.
System launcher will use the JDK with highest version among those that have an Info.plist file.
When working in a shell with alternate JDK, pick your method among existing answers (jenv, or custom aliases/scripts around /usr/libexec/java_home, etc).