Factsheet
Turns out java has been moved into brew core recently, so the correct command as of August 2022 is:
brew install java
Then check your installation by running
java -version
If the result does not looks like this:
openjdk 18.0.2 2022-07-19
OpenJDK Runtime Environment Homebrew (build 18.0.2+0)
OpenJDK 64-Bit Server VM Homebrew (build 18.0.2+0, mixed mode, sharing)
but like this:
The operation couldn’t be completed. Unable to locate a Java Runtime.
Please visit http://www.java.com for information on installing Java.
Then you also need to create a symlink for the system Java wrappers to find this JDK:
sudo ln -sfn /opt/homebrew/opt/openjdk/libexec/openjdk.jdk \
/Library/Java/JavaVirtualMachines/openjdk.jdk
As an add-on to the accepted answer: to install a certain version of Java, e.g. version 11, run:
brew install openjdk@11
And symlink it:
sudo ln -sfn /opt/homebrew/opt/openjdk@11/libexec/openjdk.jdk \
/Library/Java/JavaVirtualMachines/openjdk-11.jdk
Videos
I need a virtual environment for Java. Currently, my system is on Java 14 but for a project I am working on I need Java 8. I tried using sdkman and jenv to switch and manage virtual environments -- it's very easy and convenient but doesn't work for me (yet). Essentially, I am working at a bank and we use certificates to establish secure connections. This means certificates were set up to work with Java SDK in the home repository, but sdkman uses custom repository. I am yet to confirm whether we can fix sdkman, but going through this stuff I found out homebrew can install older versions of Java in the home repository (i.e. /Library/Java/JavaVirtualMachines).
My question is: what are the benefits of using sdkman + jenv instead of jenv + homebrew? What are the differences? Or are these the same?
So far, I've discovered that sdkman supports sbt and scala versioning which I am yet to figure out how to set up using homebrew.