Usually when a JDK is added by Brew (e.g. brew cask install adoptopenjdk) it is placed in /Library/Java/JavaVirtualMachines. I just reinstalled Scala and instead of using my JAVA_HOME env variable, it installed openjdk as well.
Why didn’t Brew put openjdk in /Library/Java/JavaVirtualMachine along with my other brew-installed JDKs?
Is there a way to configure the brew install scala formula to just use my existing JAVA_HOME?
No. It’s because that step requires sudo, which in turn tends to throw up a password prompt, which in turn breaks Homebrew’s CI build/test process for all 3,000+ formulae that depend on Java.
All these formulae are therefore configured to look for OpenJDK in /usr/local/opt/openjdk, which is under Homebrew’s control and doesn’t require any special permissions. That’s how you can run all the Java tools that Homebrew offers (including Scala), without macOS having to know about OpenJDK. You only need to do the linking step in OpenJDK’s caveat if you want to use OpenJDK with the macOS system Java wrapper, a.k.a. /usr/bin/java.
Casks like adoptopenjdk are allowed to prompt the user for their password because it’s the software’s installer package that’s doing so, and that happens only at run time, so it doesn’t affect Homebrew’s CI process.
All the Homebrew Scala wrapper scripts already respect JAVA_HOME if it’s already set, so there’s nothing for you to do there: