Multiple versions of jdk can be installed on macOS as follows:
1. Download jdk
Download different versions of jdk on the Oracle official website:
JDK download Zhihu- Security Center
Download Java11 version link
jdk11www.oracle.com/java/technologies/javase-jdk11-downloads.html
2. Install jdk
Run this installation package to install it. After installation, you will find that jdk is installed in the following directories:
/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk
/Users/Kevin/development/tools/jdk-11.0.16.1.jdk
/Users/Kevin/development/tools/jdk-17.0.4.1.jdk
3. Configure jdk
Now 3 versions of jdk have been installed, but how to determine which version is currently in effect? Or how to switch between these two versions? The configuration method is as follows:
If the currently used shell is zsh, edit the .zshrc file, and if it is bash, edit the .bash_profile file.
Add the following configuration to the above configuration file:
JAVA_HOME_8=/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home
JAVA_HOME_11=/Users/Kevin/development/tools/jdk-11.0.16.1.jdk/Contents/Home
JAVA_HOME_17=/Users/Kevin/development/tools/jdk-17.0.4.1.jdk/Contents/Home
JRE_HOME=$JAVA_HOME/jre
PATH=$PATH:$JAVA_HOME/bin
CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:.
export JAVA_HOME=$JAVA_HOME_8
export JRE_HOME
export PATH
export CLASSPATH
alias jdk8="export JAVA_HOME=$JAVA_HOME_8"
alias jdk11="export JAVA_HOME=$JAVA_HOME_11"
alias jdk17="export JAVA_HOME=$JAVA_HOME_17"
Then execute this rc file and execute the command in the terminal:
source ~/.bash_profile 或者 source ~/.zshrc
4. switch jdk
We defined aliases: jdk8 and jdk11 and jdk17.
The default configuration is jdk8
If you want to switch jdk11, execute the command in the terminal: jdk11. If you want to switch back to jdk8, execute the command: jdk8.
If you want to switch jdk17, execute the command: jdk17 in the terminal. If you want to switch back to jdk8, execute the command: jdk8.