
I use an adapted version of the switch jdk function that unsets the JAVA_HOME variable before it gets set to a new value. I prefer aliases to set the correct java version for the $1 parameter. However it can be also any arbitrary string. The $1 parameter value shall be the java version to set. The AdoptOpenJDK repository offers a function you can use in ~/.bashrc or ~/.zshrc:

You can specify the major version only: export JAVA_HOME=$(/usr/libexec/java_home -v "1.8") Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home You need to unset JAVA_HOME variable before you can set a new value for JAVA_HOME: $ unset JAVA_HOME However, setting the JAVA_HOME variable does not work this way in macOS BigSur (version 11): $ export JAVA_HOME=`/usr/libexec/java_home -v 1.8.0_275` $ echo $JAVA_HOMEĬhanging the java version in macOS prior to version 11 would be: export JAVA_HOME=`/usr/libexec/java_home -v 1.8.0_275` The JAVA_HOME variable is set to java version 15 after executing the brew install commands above. Library/Java/JavaVirtualMachines/adoptopenjdk-15.jdk/Contents/Home

The brew commands above result in these installed java versions: # Please note the upper case -V flag.ġ5.0.1 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 15" /Library/Java/JavaVirtualMachines/adoptopenjdk-15.jdk/Contents/Homeġ4.0.2 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 14" /Library/Java/JavaVirtualMachines/adoptopenjdk-14.jdk/Contents/Homeġ1.0.9.1 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 11" /Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Homeġ.8.0_275 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 8" /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home One option to install java on mac is AdoptOpenJDK with homebrew: brew install -cask adoptopenjdk


with ` brew install -cask adoptopenjdk8/11/14/15`Įxport JAVA_HOME=$(/usr/libexec/java_home -v "$1") In a nutshell # assuming versions below are installed This one is about how to change and persist the java version on macOS 11 BigSur in a shell script. Setting the java version on BigSur macOS 11 changed compare to setting the java version on earlier macOS versions.
