How to brew install java?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
When working with software development or managing server environments, Java is essential due to its robust, secure, and portable capabilities. Installing Java via Homebrew on macOS makes managing Java versions straightforward. Homebrew is a popular package manager for macOS, simplifying the installation of software and their dependencies using simple commands in the terminal. Here’s a detailed guide on how to install Java using Homebrew and how to manage different versions efficiently.
Prerequisites
Before installing Java using Homebrew, you must have Homebrew installed on your macOS. If Homebrew is not installed, open a terminal and run the following command:
This script installs Homebrew and prepares it for use.
Installing Java with Homebrew
Once Homebrew is installed, you can proceed to install Java. Homebrew makes it extremely easy to install Java and manage different versions. Follow these steps:
- Update Homebrew: It is a good practice to update Homebrew before installing any packages to ensure you’re installing the latest versions.
- Search for Java Versions: To see the versions of Java that are available for installation, use the command:
This command will list versions and related packages, such as openjdk.
- Install Java: To install the latest version of OpenJDK (which is a widely used and compatible version of Java), use the following command:
If you need a specific version, for instance, OpenJDK version 11, you would use:
- Link Java Version: If installing a version other than the latest, it will not be linked by default. To ensure commands like
javaandjavacpoint to the newly installed version, you must link it manually:
Setting JAVA_HOME Environment Variable
For some applications to work properly, or for development purposes, setting the JAVA_HOME environment variable is critical. Here’s how you can set it:
- Find Java’s Installation Path: Use the following command to locate the directory where Homebrew installed Java:
It will display paths associated with various installed Java versions.
- Set
JAVA_HOME: AddJAVA_HOMEto your shell configuration file (.bashrc, .zshrc, etc.):
This sets JAVA_HOME for OpenJDK 11 and reloads the shell configuration.
Troubleshooting Common Issues
During installation and configuration, you might encounter issues such as conflicts between different Java versions, or commands not pointing to the correct Java path. In such cases, verify the installations and links using:
These commands show the active Java and "Java Compiler (javac)" versions respectively.
Summary Table
| Action | Command |
| Install Homebrew | /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" |
| Update Homebrew | brew update |
| Install Java (latest) | brew install openjdk |
| Install specific Java version | brew install openjdk@11 |
| Link specific Java version | brew link --force --overwrite openjdk@11 |
Set JAVA_HOME | echo 'export JAVA_HOME="$(/usr/libexec/java_home -v 11)"' >> ~/.zshrc |
| Verify Java Installation | java -version |
| Verify Java Compiler | javac -version |
Installing Java through Homebrew not only simplifies the installation process but also makes it easier to manage multiple Java environments on macOS. This flexibility is crucial for developers who need to test their applications across different Java versions.

