Change Java version on Mac

[update 2021 01 12] this post is outdated.
Please refer to change default version on macOS BigSur: https://www.lotharschulz.info/2021/01/11/change-default-java-version-on-macos-11-bigsur-persist-it/

2 minutes read

Today I had to downgrade the default java version on Mac because we did start a transition of an existing service. Last time I did install java 1.8 was quite some manual effort (ls.info/…/java-1-8-and-spring-4-0-3-migration/). This time I wanted to use the existing Mac OS capabilities.

My setup was:

  • Mac OSX version: 10.14.6
  • Brew version: 2.1.10
  • Java version: OpenJDK 11.0.2, x86_64

Downgrade to Java version 1.8

Installing java 1.8 worked best with

$ brew cask install adoptopenjdk/openjdk/adoptopenjdk8

found at https://stackoverflow.com/a/56981661

That one installed java 1.8 however the system java version remained untouched.

$ java -version
openjdk version "11.0.2"

In order to set java 1.8 as default, I needed to find out which versions are installed:

$ /usr/libexec/java_home -V
Matching Java Virtual Machines (2):
11.0.2, x86_64: "OpenJDK 11.0.2"
/Library/.../openjdk-11.0.2.jdk/Contents/Home
1.8.0_222, x86_64:"AdoptOpenJDK 8"
/Library/.../adoptopenjdk-8.jdk/Contents/Home

With that, I could set the new default version (in my case as part of .zshrc setup):

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

Finally java -version returned as expected:

$ java -version
openjdk version "1.8.0_222"

found at https://stackoverflow.com/a/24657630

Conclusion

Change default java version on Mac

$ brew cask install adoptopenjdk/openjdk/adoptopenjdk8

# which java versions installed
$ /usr/libexec/java_home -V
Matching Java Virtual Machines (2):
11.0.2, x86_64: "OpenJDK 11.0.2"
/Library/.../openjdk-11.0.2.jdk/Contents/Home
1.8.0_222, x86_64:"AdoptOpenJDK 8"
/Library/.../adoptopenjdk-8.jdk/Contents/Home

# set the new default version
export JAVA_HOME=`/usr/libexec/java_home -v 1.8.0_222`

12 Comments

  1. thanks a lot! react native wouldn’t build on android until i reverted default java version from 15 to the one mentioned above. after a lot of failed attempts, you saved me!

      • do you have any idea why it gets reset back to java 15 after restarting the mac? i’d like it to remember this setting forever

        • The command
          export JAVA_HOME=`/usr/libexec/java_home -v 1.8.0_222`
          is ephemeral if issued only in the terminal. Please consider to persist the JAVA_HOME env in your .bashrc or bash_profile file like this:

          ...
          #set JAVA_HOME
          JAVA_HOME=`/usr/libexec/java_home -v 1.8.0_222`
          export JAVA_HOME
          
          #add JAVA_HOME to PATH
          export PATH=$PATH:$JAVA_HOME
          ...
          

          I hope that helps.

  2. Add this to your ~/.zshrc

    jdk() {
    version=$1
    export JAVA_HOME=$(/usr/libexec/java_home -v”$version”);
    java -version
    }

    It’ll allow you to simply type ‘jdk 1.8’ at any given time.

  3. Something must have changed with Big Sur (11.1). I installed the jdk 8 as shown here and I then set the version with these two lines in my .profile

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

    But when I run java -version it is still showing the old prior version that is being active…

    xxx@yyy ~ % java -version
    java version “1.8.0_271”
    Java(TM) SE Runtime Environment (build 1.8.0_271-b09)
    Java HotSpot(TM) 64-Bit Server VM (build 25.271-b09, mixed mode)

    Hmmm. It should be 1.8.0_275 and not 1.8.0_271.

1 Trackback / Pingback

  1. Not able to switch Java version to Java 17 – Rubin Shrestha

Leave a Reply to Lothar Schulz Cancel reply

Your email address will not be published.


*


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