Issue with Kotlin to JNI

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.

1 Like

How are you running your program? Are you using the kotlin command-line program, or using java to run a class file or a jar?

(Kotlin code needs a small runtime library, in addition to the Java standard library. Your error looks like that’s not available. Normally the kotlin program takes care of that for you, while if you’re running a .jar it’ll need to include the runtime (a ‘fat jar’), and if you’re running java on class files you’ll need to add the Kotlin runtime to the classpath manually.)

1 Like

I have been working hard with Kotlin/Multiplatform and done a lot of JNI implementation, however not on Windows. I do have a working project on macOS so far. Let me show you my files:

build.gradle.kts

library consumer:

Maybe they can serve as examples, just ask me.

Here is how I’m building and running the kotlin proram: kotlinc Sterling.kt -include-runtime -d . and then to run: java SterlingKt. I tried using kotlinc-jvm too.
I’ve tried using the java -cp flag to point to kotlin libs but not sure I’m running it right (getting an error about finding the main class) since I’m not building a jar. The program runs if it’s bare bones so it’s getting the runtime library, no? I’m not able to use runCatching or some other stuff like fun main(args: Array<String>), so is the stdlib not getting included or something? I thought I read that the stdlib was basically the runtime lib.
I’m not building a jar right now and want just a single command to build and a command to run. I’m not trying to have a lot of files to build/run the kotlin program right now, although that’s where I’m probably going next depending on this poc.
Thanks for the help,

Thanks a lot for the project reference…I’ll definitely be checking this out in greater detail in the future, but I’d rather not have a gradle build script or get into kmp stuff at the moment, although that’ll probably happen later.
I’d recommend staying away from windows lol. Trying to build this C# library and getting the jni layer to talk to it has been really difficult and has just reinforced my disgust with windows haha. If anyone else reading this has some experience with calling c# from jni, I have some questions for you heh.

Thanks,

Not declaring the external methods @JvmStatic could be one culprit.

1 Like

Adding that annotation is something I’ve tried but it doesn’t change anything. The program execution is making it to the jni layer, so I feel like the signature of the native functions is ok.

Have you done JNI in Java before coming to Kotlin, so you got it working in pure Java/Scala or similar.

Yeah, I’ve done some JNI stuff for a few android apps. and yeah, I have a pure java version of this program working.