Videos
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
In 2024, even if you can use just brew..
brew install openjdk@17
Java will be installed here:
/opt/homebrew/opt/openjdk@17/bin/java
for Apple Intel path is /usr/local/... rather than /opt/homebrew/...
For the system Java wrappers to find this JDK, symlink it with:
sudo ln -sfn /opt/homebrew/opt/openjdk\@17/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk
resp. for Intel
sudo ln -sfn /usr/local/opt/openjdk@17/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk
Now, running /usr/libexec/java_home -V should show the path to JAVA 17, something like:
17.0.9 (x86_64) "Homebrew" - "OpenJDK 17.0.9" /opt/homebrew/Cellar/openjdk@17/17.0.9/libexec/openjdk.jdk/Contents/Home
(This is what you are missing btw, if javac or javac --version does not give you a command-not-found but an The operation couldn’t be completed. Unable to locate a Java Runtime.)
In case you see the wrong version, check if the path to JAVA_HOME is set correctly. If not, you could set it by:
export JAVA_HOME=\$(/usr/libexec/java_home)
...give a try to sdkman, it's far better than brew
curl -s "https://get.sdkman.io" | bash
then open a new shell and try list to see what you could install ;-)
sdk list java
At time of writing you could use:
sdk install java 17.0.4.1-tem
Java will be installed here:
/Users/YOUR_USERNAME_HERE/.sdkman/candidates/java/17.0.4.1-tem
Java doesn't mind if you install multiple versions. This is often required; java is not backwards compatible (it tries to change little, but e.g. the java8 to java9 transition broke a ton of stuff, much of it needless and much of it not reasonably expectable or fixable by libraries and apps, so a bunch of java apps and libraries only run on java8 - just an example).
So, yes, you have installed JDK17. Also, yes, if you just run java without specifying which one you want, you so happen to get java13 here.
To see all installed javas, you can run:
/usr/libexec/java_home -V
to 'override', you can use something like (depends on which shell you're using on your mac):
export JAVA_HOME=`/usr/libexec/java_home -v 17`
(the backticks mean: Run this then take the output of it and treat that as the 'value' of the expression. here, assign it to the JAVA_HOME env var. -v 17 requests a path to java 17. The -V option lists all and is meant for your eyeballs, not for scripts. The -v option is mostly for scripting, and that's how we're using it here).
JAVA_HOME decides which java is used by some things, but the java you get when you just type java is /usr/bin/java, and that executable is actually just a wrapper that picks a java to run from amongst all installed versions. It uses JAVA_HOME to decide which java to actually run. There are wrappers for all the common commands (javac, too). You can always run e.g. which javac to see what that actually runs; you probably see /usr/bin/javac. Everything in /usr/bin is one of these wrapper thingies that looks at JAVA_HOME and then runs the binary it finds there.
Oracle has a poor record for making it easy to install and configure Java, but using Homebrew, the latest OpenJDK (Java 14) can be installed with:
brew install --cask adoptopenjdk8
For the many use cases depending on an older version (commonly Java 8), the AdoptOpenJDK project makes it possible with an extra step.
brew tap adoptopenjdk/openjdk
brew install --cask adoptopenjdk8
Existing users of Homebrew may encounter Error: Cask adoptopenjdk8 exists in multiple taps due to prior workarounds with different instructions. This can be solved by fully specifying the location with brew install --cask adoptopenjdk/openjdk/adoptopenjdk8.
Note: Oracle Java 8/9/10 is no longer available for public download (license change).
First install and update brew from Terminal:
bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew tap homebrew/cask-versions
brew update
NEW as of June 2019
To install the JDKs from AdoptOpenJDK:
brew tap adoptopenjdk/openjdk
brew install --cask adoptopenjdk8
brew install --cask adoptopenjdk9
brew install --cask adoptopenjdk10
brew install --cask adoptopenjdk11
OLD
Java 8:
brew install --cask java8
Java Latest:
brew install --cask java