Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

macos

zshrc

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?

like image 627
hitchhiker Avatar asked Oct 14 '25 19:10

hitchhiker


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

like image 182
dond Avatar answered Oct 17 '25 08:10

dond


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
like image 29
c4augustus Avatar answered Oct 17 '25 10:10

c4augustus