change java version in macOS BigSur

Viewed 23379

I can't change the java version in macOS BigSur. i have the following versions :

when I execute this command

/usr/libexec/java_home -V

i have the following versions :

Matching Java Virtual Machines (3):
    11.0.8 (x86_64) "Oracle Corporation" - "Java SE 11.0.8" /Library/Java/JavaVirtualMachines/jdk-11.0.8.jdk/Contents/Home
    1.8.261.12 (x86_64) "Oracle Corporation" - "Java" /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
    1.8.0_261 (x86_64) "Oracle Corporation" - "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_261.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/jdk-11.0.8.jdk/Contents/Home

i want to switch to java 1.8 as the default one , so I execute this command :

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

and then when I check java version :

java -version

I still got the old (higher one)

java version "11.0.8" 2020-07-14 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.8+10-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.8+10-LTS, mixed mode)

why it's not working?

7 Answers

Please use below command :-

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

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

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

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.

in my opinion the command "/usr/libexec/java_home -v" doesn't work. I solved exporting directly the java path in file .bash_profile:

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_271.jdk/Contents/Home

and then doing the command:

source .bash_profile.

After this using the command: echo $JAVA_HOME I've seen that the JVM path is set correctly.

I have added following on my .zshenv and it's working fine. Thanks munan.

## Java home
export JAVA_8_HOME=$(/usr/libexec/java_home -v1.8)
export JAVA_11_HOME=$(/usr/libexec/java_home -v11)

## Java alias
alias java8='export JAVA_VERSION=1.8'
alias java11='export JAVA_VERSION=11'

To change the java version from console, use: java8 or java11

Me too,So annoying, he caused me trouble. He seems to be solved by set the '$JAVA_VERSION'. Please use below command :-

export JAVA_VERSION=11
Related