I am writing a Kotlin Multiplatform project and everything are compiling fine when I compile it in Android Studio, however when I try to compile it in Xcode I get a compile error:
The operation couldn’t be completed. Unable to locate a Java Runtime.
Please visit http://www.java.com for information on installing Java.
I have installed Java and can in a terminal find it so it seems like the problem is in Xcode:
$ echo $JAVA_HOME
/Users/xxx/.sdkman/candidates/java/current
$ java --version
openjdk 17.0.6 2023-01-17
OpenJDK Runtime Environment Temurin-17.0.6+10 (build 17.0.6+10)
OpenJDK 64-Bit Server VM Temurin-17.0.6+10 (build 17.0.6+10, mixed mode)
I have executed kdoctor and it finds no issue with my setup:
$ kdoctor
Environment diagnose (to see all details, use -v option):
[✓] Operation System
[✓] Java
[✓] Android Studio
[✓] Xcode
[✓] Cocoapods
Conclusion:
✓ Your system is ready for Kotlin Multiplatform Mobile Development!
I had the same problem; it seems Xcode is using "/usr/libexec/java_home" to get java install dir. Running this command was also giving the 'Unable to locate Java Runtime' error:
% /usr/libexec/java_home
The operation couldn’t be completed. Unable to locate a Java Runtime. Please visit http://www.java.com for information on installing Java.
This command reads /Library/Java/JavaVirtualMachines/ directory to get java installation path. In my case, adding a symlink to the android studio java installation dir fixed the problem, and made Xcode happy :-)
sudo ln -sfn /Applications/Android\ Studio.app/Contents/jbr /Library/Java/JavaVirtualMachines/jbr
You may adapt the command above to set your java installation dir to a different location.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With