change java version in macOS BigSur

17,238

Solution 1

Please use below command :-

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

Solution 2

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

Solution 3

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

Solution 4

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.

Solution 5

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.

Share:
17,238
leonidas79
Author by

leonidas79

loving Symfony

Updated on June 14, 2022

Comments

  • leonidas79
    leonidas79 almost 2 years

    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?

  • leonidas79
    leonidas79 over 3 years
    it's not working , I still have java 11 as java version
  • jnovack
    jnovack over 3 years
    @leonidas79 after you run that command, then run set | grep JAVA what does it show you?
  • jnovack
    jnovack over 3 years
    The purposes of the backticks means that the command runs and then puts it output into the variable JAVA_HOME.
  • Anass Boukalane
    Anass Boukalane over 3 years
    When I run set | grep JAVA , I get this : JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_261.jdk‌​/Contents/home
  • Rohit Kumar
    Rohit Kumar over 3 years
    Thanks ! Worked. I was facing issue with /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home This home was set to default but I wanted to use JDK 1.8, java -version was showing 1.8 but still not able to find 1.8 sun tools. Above command set home properly and it worked.
  • Fabiano Pereira
    Fabiano Pereira over 3 years
    "unset JAVA_HOME" : Unsetting JAVA_HOME really did the trick for me, even when "set | grep JAVA" shows JAVA_HOME=' ':
  • maruf571
    maruf571 over 3 years
    Works perfectly
  • Zaibacu
    Zaibacu over 3 years
    Fixed by: export PATH=$JAVA_HOME/bin:$PATH, making binary from JAVA_HOME in front of /usr/bin/java
  • justdan0227
    justdan0227 about 3 years
    This is odd. On our build machine I did not have this. On my local machine I also did not have this but don't have the Android Gradle build issue either. Added this on the build machine and it works just fine now. Not sure why it is not needed on my local laptop
  • Koray Tugay
    Koray Tugay almost 3 years
    Yes, where does this come from and how do we modify this?
  • Zaibacu
    Zaibacu over 2 years
    this PATH is exported either in ~/.bash_profile or your other shell config file, eg. ~/.zshrc for zShell. You may need to do source ~/.bash_profile, or just open new terminal window after that