How to point /usr/libexec/java_home to custom jdk installation?

Viewed 1408

I'm on macOS monterrey and using a custom JDK 15 (I have to use a custom JDK modified for my company). When I run /usr/libexec/java_home I get this error:

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

however when I run echo $JAVA_HOME I get /Library/Java/JavaVirtualMachines/openjdk_15.0.6_15.38.18_x64. In addition running java --version return the correct JDK version.

Because /usr/libexec/java_home doesn't point to the actual JDK directory tools which use Java don't work.

Is there a way to set /usr/libexec/java_home to point to my JDK directory?

2 Answers

What c4augustus uses as a short answer almost worked for me. Uneasy part was figuring out the proper content of the linked directory – as found in another answer, /usr/libexec/java_home searches for Info.plist file located under Contents/ dir.

And that is located in greater depth of OpenJDK structure (in my case) installed in the system by Homebrew. Simple re-linking to proper directory made the trick:

sudo ln -s /usr/local/Cellar/openjdk/18.0.1.1/libexec/openjdk.jdk/ /Library/Java/JavaVirtualMachines/openjdk-18.jdk

Hope this helps someone (I needed it for VS Code being able to handle PlantUML pictures inside AsciiDoc documents).

Related