change java version in macOS BigSur

I have been confused with the same issue today after upgrade macOS to Big Sur.

There are two JDKs on my Mac, Oracle JDK 11 and Oracle JDK 8.

Then /usr/libexec/java_home always return /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home and ignore other arguments like -v 1.8

After some tests I found that, this only happened when your Mac have an Oracle JDK 1.8.X version.

So I deleted the Oracle JDK 1.8.x, /usr/libexec/java_home can return the correct JDK 11 path.

Then I tried AdoptOpenJDK 8, /usr/libexec/java_home can return its path correctly, but ignore the -v 11 arg, can't get the path of JDK 11.

It seems there ARE some bugs in java_home command, if you only have one JDK on your Mac - except Oracle JDK 8- it should works fine. If you have multiple JDKs, maybe it just return the last installed JDK path.

So either only keep one JDK installation or set $JAVA_HOME env with the absolute path will be OK.


Now there's new binary at /usr/bin/java and it gets prioritized over $JAVA_HOME/bin/java


Please use below command :-

export JAVA_HOME=`/usr/libexec/java_home -v 1.8.0_261`

you have to unset JAVA_HOME and set JAVA_VERSION then it works.

~% JAVA_VERSION=12 java -version
openjdk version "14.0.2" 2020-07-14
~% export JAVA_VERSION=12
~% java -version
openjdk version "14.0.2" 2020-07-14
~% unset JAVA_HOME
~% java -version
java version "12.0.1" 2019-04-16
~% JAVA_VERSION=13 java -version
java version "13-ea" 2019-09-17

or if you need to set JAVA_HOME you need to unset before calling /usr/libexec/java_home

function jav {
unset JAVA_HOME
export JAVA_HOME=`/usr/libexec/java_home -v $@`
}
~% jav 12
~% java -version
java version "12.0.1" 2019-04-16
~% jav 13
~% java -version
java version "13-ea" 2019-09-17