Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set JAVA_HOME in MacOS Monterey when /usr/libexec/java_home returns an error?

I recently got this new MacBook Pro (2021) 16" with MacOs Monterey and trying to install Java (openjdk11). I installed Homebrew and used brew install openjdk@11 to install java. I get following when executed java --version command,

openjdk 11.0.15 2022-04-19
OpenJDK Runtime Environment Homebrew (build 11.0.15+0)
OpenJDK 64-Bit Server VM Homebrew (build 11.0.15+0, mixed mode)

But when I execute /usr/libexec/java_home, I get the following,

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

Which I also had set as my JAVA_HOME after adding JAVA_HOME=$(/usr/libexec/java_home) to ~/.zshrc.

Even the folder /Library/Java/JavaVirtualMachines/ is empty.

Tried uninstalling according to www.java.com and then reinstall using brew but still getting the same issue.

Terminal

like image 598
Gayal Kuruppu Avatar asked Jan 23 '26 21:01

Gayal Kuruppu


2 Answers

On the Homebrew OpenJDK formula page (last check: 19 dec 2022), there is this note that says:

For the system Java wrappers to find this JDK, symlink it with sudo ln -sfn $(brew --prefix)/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk

If like me you need the JAVA_HOME variable in your shell, you can add this in your ~/.zshrc:

export JAVA_HOME=$(/usr/libexec/java_home)

(Tested with MacOS Monterey and Ventura update).

like image 188
p_147 Avatar answered Jan 25 '26 10:01

p_147


Could not solve the Original Problem then used brew install --cask temurin11 (https://adoptium.net/installation/) and installed Java with no problem.

like image 29
Gayal Kuruppu Avatar answered Jan 25 '26 11:01

Gayal Kuruppu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!