Search code examples
javajava-native-interfacefunction-pointers

Calling back into a native application from Java, via JNI


So I built this C library. It calls Java methods via the JNI, everything works fine. But how can I call back from Java into my C application?

I imagine the following: I do have a function, I take its address, I have a function pointer. I pass the pointer to Java (for example as a callback, an event handler, or anything). How can I call that function from within Java?

Is there maybe a method to dynamically and explicitly bind a native void method(); to a function pointer or something?


Solution

  • Is there maybe a method to dynamically and explicitly bind a native void method(); to a function pointer or something?

    Yes there is! You can use RegisterNatives to dynamically bind Java methods with C/C++ functions.

    To change implementation you can first call UnregisterNatives followed by a new call to RegisterNatives.