How to Install Java on MacOS Using Homebrew

Recently, I encountered a situation where I needed to install Java on my macOS machine, and the easiest way to get it up and running was through Homebrew. Whether you’re working with Java-based applications or development environments like Android Studio, having the right Java version installed is crucial. In this post, I’ll walk you through how I quickly resolved the issue using Homebrew.

Thank me by sharing on Twitter 🙏

Why Install Java with Homebrew?

The great thing about using Homebrew is that it simplifies package management on macOS. You can install, update, and switch between versions of software like Java seamlessly. If you’ve ever struggled with manually downloading and managing Java versions from Oracle’s website, Homebrew is the better alternative. It helps you stay up to date and manage Java versions easily.

Initially I got this error:

ShellScript
>java -version
The operation couldn’t be completed. Unable to locate a Java Runtime.
Please visit http://www.java.com for information on installing Java.

How to Install Java via Homebrew

Let me take you through the steps to get Java installed on your machine.

First, open your terminal and run the following command to install the latest version of OpenJDK:

ShellScript
brew install openjdk

After Homebrew completes the installation, you might need to link Java so your system can find it:

ShellScript
sudo ln -sfn $(brew --prefix openjdk)/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk

To confirm the installation, simply check the Java version:

ShellScript
java -version

Now, if you need a specific version of Java, Homebrew makes that easy too. You can install it by specifying the version:

ShellScript
brew install openjdk@22

This flexibility comes in handy when working on projects that require older or specific Java versions. You can even install Oracle’s JDK if you prefer, using the following command:

ShellScript
brew install --cask temurin

Conclusion

By using Homebrew to install Java, I was able to simplify the whole process of managing Java versions on my macOS machine. Whether you need the latest OpenJDK or a specific version, Homebrew provides a clean and efficient way to get set up without downloading installers or dealing with manual configurations. Now, switching between Java versions is as simple as a command. If you’re looking to streamline your Java setup, I definitely recommend giving this approach a try.

Share this:

1 thought on “How to Install Java on MacOS Using Homebrew”

  1. > You can even install Oracle’s JDK if you prefer, using the following command:
    > brew install –cask temurin

    Temurin, formerly known as AdoptOpenJDK, is the Eclipse Foundation’s binary build of OpenJDK. If you want Oracle’s JDK, it’s: brew install –cask oracle-jdk

    Reply

Leave a Reply