Change default Java version on macOS 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 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 the user.

I am running macOS Big Sur, version 11.1.

Links

Be the first to comment

Leave a Reply

Your email address will not be published.


*


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