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`

Lothar Schulz

Leave a Reply

Your email address will not be published. Required fields are marked *

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