2

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

Gayal Kuruppu
  • 1,261
  • 1
  • 17
  • 29
  • 1
    what happens if you type `echo $JAVA_HOME` in the terminal app? – blurfus May 31 '22 at 16:19
  • @blurfus Sorry when executed `/usr/libexec/java_home` I get this error. which led me to reinstall java(updated the question). When $JAVA_HOME is set to `JAVA_HOME=$(/usr/libexec/java_home)`, I get the same error "The operation ...." in the question. Otherwise its empty (when not set). – Gayal Kuruppu May 31 '22 at 16:28
  • Please read my comment again ;) - in the terminal, type: `>echo $JAVA_HOME` - then, let us know the output of the command. – blurfus May 31 '22 at 16:34
  • btw, you don't _execute_ `/usr/libexec/java_home` - that's the location of the JAVA HOME - – blurfus May 31 '22 at 16:36
  • @blurfus its an empty line, when I type `echo $JAVA_HOME` – Gayal Kuruppu May 31 '22 at 16:37
  • Odd. if you type `java --version` - do you still get the output above? – blurfus May 31 '22 at 16:38
  • yes, I get that – Gayal Kuruppu May 31 '22 at 16:39
  • yeah we don't execute `/usr/libexec/java_home` what I meant was, when I type it in the terminal I get that error. I am supposed to get the JAVA_HOME from that right? since we set it as our JAVA_HOME? @blurfus – Gayal Kuruppu May 31 '22 at 16:41
  • no, you are not supposed to get the JAVA_HOME from that. That's the value, not a command to retrieve the value – blurfus May 31 '22 at 16:41
  • 1
    Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/245210/discussion-between-blurfus-and-gayal-kuruppu). – blurfus May 31 '22 at 16:44

2 Answers2

2

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).

starball
  • 20,030
  • 7
  • 43
  • 238
p_147
  • 21
  • 4
1

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

Gayal Kuruppu
  • 1,261
  • 1
  • 17
  • 29