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:
>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:
Logitech M240 Silent Bluetooth Mouse, Wireless, Compact, Portable, Smooth Tracking, 18-Month Battery, for Windows, macOS, ChromeOS, Compatible with PC, Mac, Laptop, Tablets - Graphite
$12.99 (as of November 28, 2025 01:44 GMT +00:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)Nexus: A Brief History of Information Networks from the Stone Age to AI
$14.45 (as of November 26, 2025 18:35 GMT +00:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)Anker USB C to USB C Cable, Type-C Charging Cord Fast Charge(10FT,100W), for iPhone 17/17 pro, MacBook and More(Black)
$7.99 (as of November 28, 2025 01:44 GMT +00:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)brew install openjdkAfter Homebrew completes the installation, you might need to link Java so your system can find it:
sudo ln -sfn $(brew --prefix openjdk)/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdkTo confirm the installation, simply check the Java version:
java -versionNow, if you need a specific version of Java, Homebrew makes that easy too. You can install it by specifying the version:
brew install openjdk@22This 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:
brew install --cask temurinConclusion
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.



> 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