Install latest Java (OpenJDK) on Mac using Homebrew

The latest command to install JDK/Java on Mac OS

Assuming you have installed Homebrew already, otherwise, you can go to the official website of homebrew and install it first, after that, use the following commands will help you install the latest Java (OpenJDK) in your Mac

1. Do the update first

brew update

2. Check the Java in Brew

At the time of installing, the latest stable version is 15.0.2

$ brew info java

openjdk: stable 15.0.2 (bottled) [keg-only]
Development kit for the Java programming language
https://openjdk.java.net/
Not installed
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/openjdk.rb
License: Cannot Represent

3. Install Java

This will take a few minutes

brew install java

Sample Output

Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> Updated Formulae
Updated 1 formula.

==> Downloading https://ghcr.io/v2/homebrew/core/openjdk/manifests/15.0.2
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/openjdk/blobs/sha256:13e7d2b439
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sh
######################################################################## 100.0%
==> Pouring openjdk--15.0.2.big_sur.bottle.tar.gz
==> Caveats
For the system Java wrappers to find this JDK, symlink it with
  sudo ln -sfn /usr/local/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk

openjdk is keg-only, which means it was not symlinked into /usr/local,
because macOS provides similar software and installing this software in
parallel can cause all kinds of trouble.

If you need to have openjdk first in your PATH, run:
  echo 'export PATH="/usr/local/opt/openjdk/bin:$PATH"' >> ~/.profile

For compilers to find openjdk you may need to set:
  export CPPFLAGS="-I/usr/local/opt/openjdk/include"

==> Summary
🍺  /usr/local/Cellar/openjdk/15.0.2: 614 files, 324.9MB

4. Create a Symbolic Link

Refer to the output message, it said openjdk is a keg-only, which means it was not symlinked into /usr/local, so we have do do this manually for us to run java command in the terminal

sudo ln -sfn /usr/local/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk

5. Verify it

java -version

You should see something like below

openjdk version "15.0.2" 2021-01-19
OpenJDK Runtime Environment (build 15.0.2+7)
OpenJDK 64-Bit Server VM (build 15.0.2+7, mixed mode, sharing)

3 thoughts on “Install latest Java (OpenJDK) on Mac using Homebrew”

Leave a Comment