How can I upgrade from Oracle JDK 8 to OpenJDK 8 using Homebrew and be sure it will be used by default?

If I have Oracle Java SE JDK 8 installed on my mac, how can I upgrade to Open JDK 8 and be sure it will be used by default?

Motivation: Android build tools seem to need JDK 8, but according to the following links Oracle JDK 8 will not receive free updates after January 2019 for commercial use, not even security updates:

  • https://java.com/en/download/release_notice.jsp
  • https://medium.com/@javachampions/java-is-still-free-c02aef8c9e04
  • https://github.com/apache/cordova-android/issues/637

Solution 1:

Recommended prerequisite: check which versions of Java may already be installed using the following command: /usr/libexec/java_home -V

The following commands worked for me:

  • brew search openjdk # check if desired adoptopenjdk8 cask may already available
  • brew search java # (optional)
  • brew tap AdoptOpenJDK/openjdk # (assuming that the desired adoptopenjdk8 cask is not already available)
  • brew search openjdk # check that the desired adoptopenjdk8 cask is now available
  • brew install --cask adoptopenjdk8

Then the following commands confirmed that I was using the desired OpenJDK 8 version by default (with no JAVA_HOME environment variable setting):

  • java -version
  • javac -version

And the following commands confirmed that I have both Java SE 8 and OpenJDK 8 installed:

  • /usr/libexec/java_home -V
  • ls /Library/Java/JavaVirtualMachines

Note that which java and which javac show that java and javac are still run from /usr/bin.

Followup step: remove the old Java SE SDK from /Library/Java/JavaVirtualMachines, as documented in: https://docs.oracle.com/javase/8/docs/technotes/guides/install/mac_jdk.html#A1096903

AdoptOpenJDK 8 package notes May 2019: adoptopenjdk8 was also added to homebrew/cask-versions, and this led to some issues: https://github.com/AdoptOpenJDK/homebrew-openjdk/issues/106

There is now a proposal to rename adoptopenjdk8 to openjdk8 in homebrew/cask-versions: https://github.com/Homebrew/homebrew-cask-versions/pull/7375

If this proposal is merged, this answer will likely be updated to recommend installing the renamed cask.

See also (some references):

  • https://stackoverflow.com/questions/44632292/how-to-install-specific-java-version-using-homebrew - discovered several months after I wrote this answer
  • https://dzone.com/articles/install-openjdk-versions-on-the-mac - discovered after I wrote this answer, mostly good but shows some formulae that I did not see
  • How can I install java openjdk 8 on High Sierra
  • https://discourse.brew.sh/t/how-to-install-openjdk-with-brew/712
  • https://github.com/AdoptOpenJDK/homebrew-openjdk#other-versions
  • https://medium.com/notes-for-geeks/java-home-and-java-home-on-macos-f246cab643bd
  • How to update Java from 8 Update 31 to 8 Update 51 when install fails? - with an old answer with a very helpful link through which I found the page with Oracle JDK uninstall directions
  • Does Mountain lion download Apple's JDK or the OpenJDK when attempting to load a Java app? - interesting question from July 2012