Mac (Brew + Jenv)
Install JDK versions with brew
brew install openjdk@21
brew install openjdk@17
# see installed versions
brew list
Note
These JDKs are stored in
/opt/homebrew/opt
Warning
If you have IntelliJ Idea installed, the applications native JDK is stored in
~/$HOME/Library/Java/JavaVirtualMachines
So if you want to have all your JDKs in one directory, have a symbolic link for each brew installed JDK to
~/$HOME/Library/Java/JavaVirtualMachines
Manage versions with jenv
# Install jenv
brew install jenv
# set PATH and init jenv when shell session starts
echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(jenv init -)"' >> ~/.zshrc
jenv doctor
# If JAVA_HOME is not set
jenv enable-plugin export
# For each JDK that you want jenv to manage
jenv add /path/to/jdk
# Example
jenv add ~/$HOME/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
# list JDK versions managed by jenv
jenv versions
# configure global version
jenv global openjdk@21
# configure local version (per directory)
jenv local openjdk@17
# configure shell instance version
jenv shell openjdk@17
# check which JDK version with
export $JAVA_HOME