Change Java version on Mac 11 BigSur & persist it

change default Java version on BigSur macOS 11 and persist it

~ 5 minutes read

Setting the java version on BigSur macOS 11 changed compare to setting the java version on earlier macOS versions. This one is about how to change and persist the java version on macOS 11 BigSur in a shell script.

In a nutshell

# assuming versions below are installed 
# e.g. with `brew install --cask adoptopenjdk8/11/14/15`
javahome() {
  unset JAVA_HOME 
  export JAVA_HOME=$(/usr/libexec/java_home -v "$1");
  java -version
}

alias j1.8='javahome 1.8'
alias j11='javahome 11'
alias j14='javahome 14'
alias j15='javahome 15'


function javahome & aliases in action:

Java Version Details

One option to install java on mac is AdoptOpenJDK with homebrew:

brew install --cask adoptopenjdk
brew tap AdoptOpenJDK/openjdk
brew install --cask adoptopenjdk
brew install --cask adoptopenjdk8
brew install --cask adoptopenjdk11
brew install --cask adoptopenjdk14
brew install --cask adoptopenjdk15

The brew commands above result in these installed java versions:

# Please note the upper case -V flag.
$ /usr/libexec/java_home -V
Matching Java Virtual Machines (4):
    15.0.1 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 15" /Library/Java/JavaVirtualMachines/adoptopenjdk-15.jdk/Contents/Home
    14.0.2 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 14" /Library/Java/JavaVirtualMachines/adoptopenjdk-14.jdk/Contents/Home
    11.0.9.1 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 11" /Library/Java/JavaVirtualMachines/adoptopenjdk-11.jdk/Contents/Home
    1.8.0_275 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 8" /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/adoptopenjdk-15.jdk/Contents/Home

The JAVA_HOME variable is set to java version 15 after executing the brew install commands above.

$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/adoptopenjdk-15.jdk/Contents/Home

Changing the java version in macOS prior to version 11 would be:

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

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
/Library/Java/JavaVirtualMachines/adoptopenjdk-15.jdk/Contents/Home

You need to unset JAVA_HOME variable before you can set a new value for JAVA_HOME:

$ unset JAVA_HOME
$ export JAVA_HOME=$(/usr/libexec/java_home -v "1.8.0_275")
$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home

You can specify the major version only:

export JAVA_HOME=$(/usr/libexec/java_home -v "1.8")

The AdoptOpenJDK repository offers a function you can use in ~/.bashrc or ~/.zshrc:
Switch between different JDK versions .

The $1 parameter value shall be the java version to set. However it can be also any arbitrary string.

I prefer aliases to set the correct java version for the $1 parameter. 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 also prefer oh my z, so the code below (w/o comments) is is part of my .zshrc:

javahome() {
  # unset JAVA_HOME
  unset JAVA_HOME 
  # set JAVA_HOME to the value of $1
  export JAVA_HOME=$(/usr/libexec/java_home -v "$1");
  # output of java version to terminal
  java -version
}

# aliases for all of my installed java versions
alias j1.8='javahome 1.8'
alias j11='javahome 11'
alias j14='javahome 14'
alias j15='javahome 15'

This code snippet can be used in other shell init files like ~/.bashrc as well. Setting the java version as part of a shell init file persists the java version for this user.

I am running macOS Big Sur, version 11.1.

Links

5 Comments

  1. Thanks a lot! You saved me once again. React native will not be able to run on Android if you use Java 15. You need to set Java 1.8 to be the default version.

  2. I’ve got some old java tools that require Java 1.6 I got that to work in Catalina, but have had no luck with Big Sur. Any ideas? I’ve managed to run the installer, but it’s not showing up with /usr/libexec/java_home -V (though other versions are). Changing the JAVA_HOME (As above) behaves as though that version wasn’t there (selects the default).
    I know that I need to move off Java 1.6, but not having it available is going to slow that move down.

    • Hi Ben,

      The following steps work for me on macOS Big Sur, version v11.2.2.

      I installed Java 1.6 like so:

      $ brew tap homebrew/cask-versions
      ...
      Tapped .... casks
      $ brew install java6
      ...
        java6 was successfully installed!

      Still JAVA_HOME pointed not to Java 1.6:

      $ echo $JAVA_HOME
      /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
      $ java -version
      openjdk version "1.8.0_282"

      Java 1.6 is installed in /Library/Java/JavaVirtualMachines/ next to the other java/jdk installations I have on my machine.

      $ cd /Library/Java/JavaVirtualMachines/
      $ ls
      1.6.0.jdk adoptopenjdk-11.jdk adoptopenjdk-14.jdk adoptopenjdk-8.jdk

      With that in place, I set JAVA_HOME like this:

      $ unset JAVA_HOME
      $ echo $JAVA_HOME
      
      $ export JAVA_HOME=/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
      $ echo $JAVA_HOME
       /Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

      This corresponding java version is

      $ java -version
      java version "1.6.0_65"
      Java(TM) SE Runtime Environment (build 1.6.0_65-b14-468)
      Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-468, mixed mode)

      Let me know if that helped.

      —-

      Afterwards I set JAVA_HOME back to java1.8 on my laptop:

      $ j1.8
      openjdk version "1.8.0_282"
      OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_282-b08)
      OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.282-b08, mixed mode)
      $ echo $JAVA_HOME
      /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home

      I did not adapt the javahome() function on my laptop because I consider the java1.6 downgrade an exception.

      Lothar

  3. Even after setting the unsetting and then setting the JAVA_HOME to 1.8, my version shows as Java 15. Why is this happening?

    $ unset JAVA_HOME
    $ export JAVA_HOME=$(/usr/libexec/java_home -v “1.8.”)
    $ echo $JAVA_HOME
    /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
    $ java -version
    openjdk version “15.0.2” 2021-01-19
    OpenJDK Runtime Environment (build 15.0.2+7)
    OpenJDK 64-Bit Server VM (build 15.0.2+7, mixed mode, sharing)

    • Please share the output of the two following commands:

      java -version
      
      $JAVA_HOME/bin/java -version
      

      On my laptop it is:

      $ java -version               
      openjdk version "1.8.0_282"
      OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_282-b08)
      OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.282-b08, mixed mode)
      
      $ $JAVA_HOME/bin/java -version
      openjdk version "1.8.0_282"
      OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_282-b08)
      OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.282-b08, mixed mode)
      

      Maybe your PATH is set in a way that you machine always returns java version 15
      when you invoke $ java -version .

Leave a Reply

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.