How to brew install java?
Categories:
Installing Java on macOS with Homebrew

Learn how to efficiently install and manage multiple Java Development Kit (JDK) versions on your macOS system using Homebrew, the popular package manager.
For macOS users, Homebrew is the de facto standard for installing and managing software packages, including various versions of the Java Development Kit (JDK). This guide will walk you through the process of installing Java using Homebrew, managing different versions, and setting up your environment variables correctly. Whether you're a developer needing specific JDK versions for projects or just getting started with Java, Homebrew simplifies the entire process.
Prerequisites: Installing Homebrew
Before you can use Homebrew to install Java, you need to ensure Homebrew itself is installed on your macOS system. If you already have Homebrew, you can skip this step. Otherwise, open your Terminal application and run the following command. This script will download and install Homebrew, along with its dependencies, and guide you through any necessary setup steps.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Command to install Homebrew on macOS.
eval
commands provided by the installer.Installing Specific Java Versions
Homebrew uses 'casks' to manage GUI applications and larger binaries like JDKs. To install a specific version of Java, you'll use the brew install --cask
command followed by the JDK package name. Common packages include temurin
(Eclipse Adoptium's OpenJDK builds) or openjdk
(for the latest stable OpenJDK). You can also specify older versions if needed.
# Install the latest LTS version (e.g., Java 17 or 21)
brew install --cask temurin
# Install a specific version, for example, Java 11
brew install --cask temurin11
# Install Java 8
brew install --cask temurin8
Examples of installing different Java versions using Homebrew.
/Library/Java/JavaVirtualMachines/
and creates symlinks in /usr/local/opt/
or /opt/homebrew/opt/
(for Apple Silicon) for easy access.Managing Multiple Java Versions with JAVA_HOME
When you have multiple JDKs installed, you'll need a way to switch between them. The JAVA_HOME
environment variable tells your system and applications which JDK to use. While Homebrew installs JDKs, it doesn't automatically set JAVA_HOME
. You'll typically manage this in your shell's configuration file (e.g., .zshrc
or .bash_profile
).
flowchart TD A[Start] --> B{Check Current JAVA_HOME}; B --> C{Multiple JDKs Installed?}; C -- Yes --> D[Choose Desired JDK Version]; D --> E["Update .zshrc/.bash_profile"]; E --> F["Set JAVA_HOME to JDK Path"]; F --> G["Source Profile (e.g., source ~/.zshrc)"]; G --> H[Verify JAVA_HOME and Java Version]; C -- No --> H; H --> I[End];
Workflow for managing JAVA_HOME with multiple JDK installations.
To set JAVA_HOME
, you need to know the installation path of your desired JDK. Homebrew typically installs JDKs into /Library/Java/JavaVirtualMachines/
or /opt/homebrew/Cellar/temurin<version>/<version>/libexec/openjdk.jdk/Contents/Home
for Apple Silicon. You can find the exact path by listing the contents of these directories or using ls -l /usr/local/opt/
(or /opt/homebrew/opt/
).
# Example for Java 17 (Temurin)
export JAVA_HOME="/Library/Java/JavaVirtualMachines/temurin-17.jdk/Contents/Home"
# Example for Java 11 (Temurin)
# export JAVA_HOME="/Library/Java/JavaVirtualMachines/temurin-11.jdk/Contents/Home"
# Add this to your ~/.zshrc or ~/.bash_profile
# Then run: source ~/.zshrc (or ~/.bash_profile)
# Verify the current Java version
java -version
echo $JAVA_HOME
Setting the JAVA_HOME environment variable and verifying the installation.
export JAVA_HOME
line active in your shell profile to avoid conflicts. Comment out or remove previous settings if you're switching versions.Listing and Uninstalling Java Versions
Homebrew provides commands to list all installed casks and to uninstall specific ones. This helps in keeping your system clean and managing your JDK installations effectively.
# List all installed casks (including JDKs)
brew list --cask
# Uninstall a specific Java version, e.g., Temurin 11
brew uninstall --cask temurin11
Commands to list and uninstall Java versions installed via Homebrew.
By following these steps, you can confidently install, manage, and switch between different Java versions on your macOS system using Homebrew, streamlining your development workflow.