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:
[Apple MFi Certified] 6Pack 3/3/6/6/6/10 FT iPhone Charger Nylon Braided Fast Charging Lightning Cable Compatible iPhone 14 Pro/13 mini/13/12/11 Pro MAX/XR/XS/8/7/Plus/6S/SE/iPad
$11.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.)Anker USB to USB C Cable [2 Pack, 3FT], USB A to USB C Charger Cord for Samsung Galaxy S10 S10+, LG V30, Beats Fit Pro and More (USB 2.0, Black)
$8.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.)Escape from a Video Game: The Complete Series
$24.99 (as of December 21, 2024 19:39 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