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 M510 Wireless Computer Mouse for PC with USB Unifying Receiver - Graphite
$19.99 (as of December 21, 2024 08:38 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.)4Pack [Apple MFi Certified] Charger Lightning to USB Charging Cable Cord Compatible iPhone 14/13/12/11 Pro/11/XS MAX/XR/8/7/6s Plus,iPad Pro/Air/Mini,iPod Touch
$7.99 (as of December 21, 2024 08:38 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.)iPhone Charger 3Pack 10 FT Apple MFi Certified Lightning Cable Fast Charging iPhone Charger Cord Compatible with iPhone 14 13 12 11 Pro Max XR XS X 8 7 6 Plus SE iPad and More
$7.99 (as of December 21, 2024 08:38 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 openjdk
After 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.jdk
To confirm the installation, simply check the Java version:
java -version
Now, if you need a specific version of Java, Homebrew makes that easy too. You can install it by specifying the version:
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:
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.
> 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