Java

There is no AI here. All content is human-authored and tested for accuracy.

Brew Install Java - Formula Method

BBrew install Java with the OpenJDK Homebrew formula. How to install the Java JDK with brew, add the required symlink, and configure Java correctly on macOS.

Homebrew, the software package manager for macOS, is the easiest way to install Java on your Mac. But you need to choose: Homebrew offers two different ways to install Java, the cask method and the formula method.

Most developers prefer the cask method. It's the easiest way to install Java on macOS. See the guide:

Unless you specifically want the formula method, I recommend you use the other guide to install Java. Using the cask method, Java works immediately after installation with no additional configuration.

However, some developers want to use the formula method described here. If you've used Homebrew to install other software languages and you're comfortable with additional configuration steps, you can use the formula method.

For an overall introduction, see my guide Java on Mac to understand Java vendors and distributions, versions, development tools, and other ways to install Java. If you're not sure whether Java is already on your Mac, see Check Java Version on Mac.

Before you get started

You'll need a terminal application to install Java via Homebrew formulas. Apple includes the Mac terminal but I prefer Warp Terminal. Warp is an easy-to-use terminal application, with AI assistance to help you learn and remember terminal commands. Download Warp Terminal now; it's FREE and makes coding easier when working with Java.

Two Paths: Formulas vs Casks

Homebrew provides two distinct mechanisms for Java installation. This tutorial focuses on the formula method.

The formula approach places Java within Homebrew's internal directory hierarchy (called the Cellar). After installation, you must create a symlink so macOS can locate the JDK. You might prefer this approach as it is consistent with other software languages installed with Homebrew, but recognize that the formula approach is primarily intended for Homebrew system use, installing the Java openjdk as a dependency for other tools such as Maven.

The cask approach drops Java directly into /Library/Java/JavaVirtualMachines/ which is the standard macOS location for Java. Everything works out of the box with zero extra steps. Most users find this simpler (see instructions in Brew Install Java With the Cask Method).

Homebrew's Java Formula

Formulas are Homebrew scripts that control the installation of software packages within Homebrew's internal directories. Casks are scripts that install vendor-supplied binaries outside of the Homebrew internal directories. The formula definition (a Ruby file in the core repo) is maintained by the Homebrew project and contributors. For your macOS version and CPU, the formula will install a prebuilt bottle (binary), if available, but can also compile Java from source using various build tools installed with Homebrew.

The Homebrew Java java or openjdk formula installs OpenJDK, the official reference implementation of Java SE, developed collaboratively by many companies and individuals, and free of proprietary licensing constraints.

Important characteristics:

  • Formula Aliases: The formula names java, openjdk, and openjdk@25 are all aliases for the same formula
  • Keg-only status: Homebrew installs Java but skips automatic system-wide linking, called "keg-only" in the Homebrew terminology
  • Latest stable version: Java 25
  • Other versions: Separate formulas exist for openjdk@21, openjdk@17, openjdk@11, and openjdk@8 versions

The base openjdk formula tracks the newest Java release, which you should use unless your team or your university course specifically requires an older version. See Java Latest Version and Compare Java 21 to Java 25 for more information about versions.

Before You Begin

If you do not have Homebrew yet, see our guide: Install Homebrew.

Identify Your Processor Architecture

Homebrew uses different base directories depending on your chip:

  • Apple M-series Silicon chips: /opt/homebrew
  • Intel processors: /usr/local

This matters because symlink paths differ between architectures. Examples throughout this guide include both variants.

Read Java on Apple Silicon or Intel Macs to learn how to check.

Check if Homebrew is Installed

Learn How to Open Terminal in Mac and run:

$ brew --version

You should see output like Homebrew 5.0.8. If Homebrew is not installed, you will see zsh: command not found: brew. See Zsh: command not found: brew if you think Homebrew is already installed. Otherwise, see our instructions to Install Homebrew.

Update Homebrew

Before installing Java, update Homebrew to get the latest package information:

$ brew update
==> Updating Homebrew...

Stale metadata can trigger "formula not available" errors during installation.

Which Version Should You Install

Java 25 is the current Long-Term Support (LTS) release, meaning it receives security updates until at least September 2033.

  • Java 25 – Current LTS (recommended), supported until September 2033
  • Java 21 – Previous LTS, supported until September 2031
  • Java 17 – Extended LTS, supported until September 2029
  • Java 11 – Extended LTS, for legacy projects
  • Java 8 – Extended LTS, for legacy systems only

Use Java 25 unless your team or your university course specifically requires an older version. See Java Latest Version and Compare Java 21 to Java 25 for more information about versions.

Brew Install Java

Now you can install Java. For Java 25, run:

$ brew install openjdk@25

Homebrew retrieves the package, validates its integrity, and unpacks it into the Cellar. Expect the process to take a couple of minutes.

Alternatively, you can use the formula alias for the latest version:

$ brew install java

Running brew install java triggers installation of the generic openjdk formula which is currently Java 25.

Examine the Caveats

Once installation finishes, Homebrew prints "caveats." The caveats look cryptic but it's important to check the post-install guidance. You might see:

==> Caveats
For the system Java wrappers to find this JDK, symlink it with
  sudo ln -sfn /opt/homebrew/opt/openjdk@25/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-25.jdk

openjdk@25 is keg-only, which means it was not symlinked into /opt/homebrew,
because this is an alternate version of another formula.

If you need to have openjdk@25 first in your PATH, run:
  echo 'export PATH="/opt/homebrew/opt/openjdk@25/bin:$PATH"' >> ~/.zshrc

Paths vary by architecture. Copy the commands from your actual terminal output rather than this documentation.

If You Miss the Caveats

If you need to see the caveats later, you can display them again:

$ brew info openjdk@25

This shows version details, dependency information, and the critical symlink instructions.

Configuring the Symlink (Mandatory Step)

Without this configuration, Java remains invisible to macOS. The symlink bridges Homebrew's Cellar location and the system's expected JDK directory.

Why This Step Matters

The openjdk formula carries "keg-only" status. Homebrew installs it locally but doesn't make any system configuration. MacOS always searches /Library/Java/JavaVirtualMachines/ when looking for JDKs. The symlink makes Homebrew's installation visible there.

If you skip this important step, you will encounter:

  • java commands returning "command not found"
  • The /usr/libexec/java_home utility failing to detect any runtime
  • Build tools and IDEs unable to locate a JDK

To set the symlink, you'll need to use sudo which requires administrator privileges. If it's your own Mac, the administrator password is the password you use to unlock your Mac. Administrator privileges are necessary because /Library/Java/JavaVirtualMachines/ is a protected system directory.

Symlink Command for Apple Silicon

On M-series Macs, execute:

$ sudo ln -sfn /opt/homebrew/opt/openjdk@25/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-25.jdk

Symlink Command for Intel Processors

On Intel-based hardware, use:

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

Provide your password when prompted (you won't see it displayed).

Breaking Down the Command

Here's what the command does:

  • sudo — Elevates to administrator permissions
  • ln — Creates a filesystem link
  • -s — Specifies a symbolic (soft) link rather than a hard link
  • -f — Replaces any pre-existing link at the destination
  • -n — Prevents following existing symlinks during creation
  • First path — Source location inside Homebrew's structure
  • Second path — Target location where macOS expects JDKs

Confirm the Symlink Exists

You'll need to close and reopen your terminal.

Then check the result:

$ ls -la /Library/Java/JavaVirtualMachines/

Look for an entry like:

$ ls -la /Library/Java/JavaVirtualMachines/
openjdk-25.jdk -> /opt/homebrew/opt/openjdk@25/libexec/openjdk.jdk

Set JAVA_HOME

With the symlink in place, basic java commands work. For casual terminal usage, you typically do not need JAVA_HOME once the symlink exists. But build tools like Maven or Gradle often require the JAVA_HOME environment variable pointing to your JDK root. Android Studio and other Java-centric IDEs may also check for this variable.

To set JAVA_HOME, use this shortcut to add the environment variable to your shell configuration file .zprofile file (see .zshrc or .zprofile):

$ echo 'export JAVA_HOME=$(/usr/libexec/java_home)' >> ~/.zprofile
$ source ~/.zprofile

Read Set JAVA_HOME on Mac for complete configuration instructions. For advanced PATH setup, see Java PATH on Mac. See the articles Mac Shell Configuration and Set Mac Path for instructions about editing shell configuration files.

Verify the Installation

Here is how to check if Java is installed correctly. See Check Java Version on Mac for a full set of checks.

Brew list

To confirm Homebrew installed the package:

$ brew list openjdk@25

Check the Java Version

$ java -version

You should see output like:

$ java -version
openjdk version "25.0.1" 2025-10-21
OpenJDK Runtime Environment Homebrew (build 25.0.1)
OpenJDK 64-Bit Server VM Homebrew (build 25.0.1, mixed mode)

Notice the "Homebrew" label confirming the build origin.

Additional Verification Commands

Check the Java compiler to confirmn you have the developer JDK:

$ javac -version
javac 25.0.1

Checking the binary path will show whether Java is available and which location it runs from:

$ which java

List All Installed JDKs

macOS provides a utility to list every JDK it recognizes:

$ /usr/libexec/java_home -V

Sample output:

$ /usr/libexec/java_home -V
Matching Java Virtual Machines (1):
    25.0.1 (arm64) "Homebrew" - "OpenJDK 25.0.1" /Library/Java/JavaVirtualMachines/openjdk-25.jdk/Contents/Home

Absence from this list signals a symlink problem.

Is JAVA_HOME set?

If you've Set JAVA_HOME on Mac in your shell configuration file and reset your terminal application, you can verify it:

$ echo $JAVA_HOME

Inspecting Homebrew's Records

View formula metadata:

$ brew info openjdk@25

Display the Cellar path:

$ brew --prefix openjdk@25

Adding More Java Versions

Multiple JDKs can coexist on one machine. Assign each a distinct symlink name to prevent collisions.

OpenJDK Versions Available via Homebrew

  • Java 25 – Formula openjdk@25, current LTS, recommended for new work
  • Java 21 – Formula openjdk@21, previous LTS, widely deployed
  • Java 17 – Formula openjdk@17, extended LTS, many legacy projects
  • Java 11 – Formula openjdk@11, extended support, older codebases
  • Java 8 – Formula openjdk@8, legacy only, Intel Macs exclusively

Installing a Second Version

To add Java 21 alongside an existing Java 25 installation:

$ brew install openjdk@21

Then establish its symlink (Apple Silicon shown):

$ sudo ln -sfn /opt/homebrew/opt/openjdk@21/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-21.jdk

Observe the unique destination name openjdk-21.jdk. Each version requires a different target to avoid overwriting.

Read Compare Java 21 to Java 25 for a detailed comparison between the two newest LTS versions.

Apple Silicon and Java 8 Incompatibility

The openjdk@8 formula lacks ARM64 support. Developers needing Java 8 on M-series Macs should use the Azul Zulu cask:

$ brew install --cask zulu@8

Update Java

Java receives quarterly security updates. Keep your installation current. Read Update Java on Mac for more about keeping Java current.

Check for Updates

$ brew outdated

Upgrade Java

$ brew upgrade openjdk@25

Your symlink remains valid after upgrades. Homebrew updates the Cellar contents while the versioned opt path (/opt/homebrew/opt/openjdk@25/) stays unchanged.

Clean Up Old Downloads

Free disk space by clearing old downloads:

$ brew cleanup

See Housekeeping for Homebrew for more on how to keep Homebrew and its packages up to date.

Uninstall Java

Proper cleanup involves deleting both the symlink and the formula.

Delete the Symlink First

$ sudo rm /Library/Java/JavaVirtualMachines/openjdk-25.jdk

Uninstall the Formula

$ brew uninstall openjdk@25

If you originally ran brew install java:

$ brew uninstall java

Confirm Removal

Check that no JDKs remain visible:

$ /usr/libexec/java_home -V

Inspect the directory:

$ ls /Library/Java/JavaVirtualMachines/

Final Cleanup

$ brew cleanup

Read Uninstall Java on Mac for complete removal instructions including manual installations.

Troubleshooting

Some issues are predictable. Here are fixes for common problems.

java: command not found after installation

Root cause: The symlink does not exist.

Resolution: Execute the sudo ln -sfn command matching your architecture (see the symlink section). Afterward, close and reopen your terminal.

java_home Reports "Unable to locate a Java Runtime"

Root cause: Missing or malformed symlink.

Resolution:

  1. Confirm the formula exists: brew list openjdk@25
  2. Inspect the link: ls -la /Library/Java/JavaVirtualMachines/
  3. Recreate the symlink using the correct architecture-specific path

Installed JDK Does Not Appear in java_home Output

Root cause: The symlink target is absent from /Library/Java/JavaVirtualMachines/.

Resolution: Create the symlink. The java_home utility exclusively scans that directory.

Java 8 Installation Fails on Apple Silicon

Root cause: ARM64 builds do not exist for openjdk@8.

Resolution: Switch to the Zulu cask:

$ brew install --cask zulu@8

Read Unable to Locate a Java Runtime for detailed troubleshooting of path and detection issues.

What's Next

Java is now operational on your Mac. If you work on multiple projects requiring different Java versions, version management tools can automate switching between runtimes. Read Java Version Managers to learn about jEnv, SDKMAN, and other tools for switching Java versions.

My mac.install.guide is a trusted source of installation guides for professional developers. Take a look at the Mac Install Guide home page for tips and trends and see what to install next.