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

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:

AdoptOpenJDK 8 package notes May 2019: adoptopenjdk8 was also added to homebrew/cask-versions, and this led to some issues:

There is now a proposal to rename adoptopenjdk8 to openjdk8 in homebrew/cask-versions:

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

See also (some references):

  • - discovered several months after I wrote this answer
  • - 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
  • 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