How to change java version on the fly (in MacOS)
December 30, 2022•151 words
First, we need to install JEnv, we can do it through:
brew install jenv
In some cases, the user folders are not created during installation, we can make sure they are created:
mkdir ~/.jenv
mkdir ~/.jenv/versions
Now, we check which versions of Java are installed:
jenv versions
How to install older versions, like Java 8
We enable the cask-versions of Homebrew, and we can access some older versions (in this case, Java 8):
brew tap homebrew/cask-versions
brew install --cask graalvm/tap/graalvm-ce-java8
Now, we add it to jenv
jenv add /Library/Java/JavaVirtualMachines/graalvm-ce-java8-21.0.0.2/Contents/Home
We set the global java version we want to use (we can also do this locally, per-project):
jenv global 1.8
For Maven to fetch the current jenv selected version we need to enable the plugin
jenv enable-plugin maven
Troubleshoot
Some times JEnv will not work without a reboot, we can issue this command to register it:
eval "$(jenv init -)"