Pre-requisites
Homebrew: install Homebrew so that you can install new apps on Mac via commandline
Install Jenv
This app will let you switch the active JDK easily
Follow the steps in: https://www.jenv.be/
My steps are:
- Install:
brew install jenv
- Run the following (I am using Zsh, for Bash check https://www.jenv.be/):
echo ‘export PATH=”$HOME/.jenv/bin:$PATH”’ >> ~/.zshrc
echo ‘eval “$(jenv init -)”’ >> ~/.zshrc
- Run this to allow jEnv to set the JAVA_HOME:
jenv enable-plugin export
Install OpenJDK@17
brew install openjdk@17
Symlink it:
sudo ln -sfn /usr/local/opt/openjdk@17/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-17.jdk
Configure Jenv
Register the installed JDK with Jenv:
jenv add /Library/Java/JavaVirtualMachines/openjdk-17.jdk/Contents/Home
Verify:
jenv versions
JDK 17 is shown as registered with Jenv.
Switch JDK version
Now you can switch the JDK version via Jenv:
jenv global 17.0.3
Verify that JAVA_HOME is correctly set: