I’ve got a project where I’m trying to call a c++, as a dll on Windows, from java/kotlin. I got things working with Java, but since I love kotlin, I wanted to switch but I’m running into a lot of issues. The latest issue looks like a problem with the execution returning from the jni layer. Here is my kotlin class:
import java.lang.IllegalStateException
class Sterling constructor() {
init {
try {
println("Loading library")
System.loadLibrary("Sterling")
} catch (exception: Exception) {
throw IllegalStateException(exception)
}
}
fun testMe(num: Int): Int = dotnet_testMe(num)
fun showDialog() = dotnet_showDialog()
external fun dotnet_testMe(num: Int): Int
external fun dotnet_showDialog()
}
fun main() {
System.setProperty("java.library.path", System.getProperty("user.dir"))
println("About to call C# functions from Java over the MS COM")
val instance = Sterling()
println("C# test me: " + instance.testMe(3))
println("C# show dialog now")
instance.showDialog()
}
Things fail when calling instance.testMe(3))
, which looks like
JNIEXPORT jint JNICALL Java_Sterling_dotnet_1testMe
(JNIEnv *env, jclass thisObj, jint num) {
printf("hello cpp, test me\n");
return 43;
}
Here is the output from the program:
About to call C# functions from Java over the MS COM
Loading library
hello cpp, test me
Exception in thread "main" java.lang.NoClassDefFoundError: kotlin/jvm/internal/Intrinsics
at SterlingKt.main(Sterling.kt:27)
at SterlingKt.main(Sterling.kt)
Caused by: java.lang.ClassNotFoundException: kotlin.jvm.internal.Intrinsics
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 2 more
Ever since I started building things with Kotlin, I’ve been continually running into this NoClassDefFoundError: kotlin/jvm/internal/Intrinsics
error, and it’s getting very annoying. This error doesn’t give any clues about what the actual issue is. This is a pretty simple class and it’s taken me hrs to get to this point. I’m considering writing up some bugs because it doesn’t seem right that I’m having such a hard time, and it seems like every error is this missing Intrinsics class. If I put the external functions in a companion object, there’s an unsatisfied link error.
Like I mentioned, Java isn’t having any issues. Not only has Java been quicker to set up, but Java is way faster at compiling too.
I’m currently using java 11 and kotlin 1.6.20-RC, and running on Windows 10. Any insights would be appreciated.