Change default Java version on Mac

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`

6 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.

Leave a Reply

Your email address will not be published.


*


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