hitchhiker
hitchhiker

Reputation: 1319

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

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?

Upvotes: 4

Views: 6854

Answers (2)

dond
dond

Reputation: 158

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

Upvotes: 4

c4augustus
c4augustus

Reputation: 306

See these questions for a deep dive into this topic:

How can I change Mac OS's default Java VM returned from /usr/libexec/java_home

How to set or change the default Java (JDK) version on macOS?

Here is a short answer that works:

sudo ln -s (absolute path to your JVM) /Library/Java/JavaVirtualMachines/(symlink name)

In my case, it points to the JVM in the latest Android Studio:

sudo ln -s /Applications/Android\ Studio.app/Contents/jre /Library/Java/JavaVirtualMachines/android-studio-jre

Upvotes: 3

Related Questions