Skip to content

Example for making C libraries execute callbacks that call java code.

License

Notifications You must be signed in to change notification settings

areese/jni_callback

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This is example code for having a c library execute a java method as a callback.

com.yahoo.jni.example.ExampleJni This class calls the JNI and gives it a Java function to use as a callback

com.yahoo.jni.example.JniLibraryLoader This class is because I'm sick of wondering why someone can't load the .so

com.yahoo.jni.example.RunCallback Is the main class. java -cp target/class -Djava.library.path=target/native/x86_64-darwin-clang com.yahoo.jni.example.RunCallback

com.yahoo.jni.example.ExampleJniAccess This is the class containing the native functions

com.yahoo.jni.example.CallbackImplementation This class contains the java function that the c library will call in the end.

calback_support.cpp: This file contains the functions that help find and attach the jvm. Currently assumes no other threads are in play.

callbacklib.cpp/h: This is the "library" in our simple case it's a single function call: magicCFunction.

com_yahoo_jni_example_ExampleJniAccess.cpp This is the c behind ExampleJniAccess. This binds the java to the c. com_yahoo_jni_example_ExampleJniAccess.h autogenerated with javah -cp target/classes com.yahoo.jni.example.ExampleJniAccess I typically copy this to start the cpp file.

jni_helper_defines.h This is a set of macros you'll also find here: https://github.com/yahoo/mdbm/blob/master/src/java/jni_helper_defines.h these originated in as swig helper macros, and have since become useful outside of that.

Important reading:

Invocation api spec: http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/invocation.html

Caveats about using pthread destructors to detach threads: http://stackoverflow.com/questions/20325792/java-thread-leaks-when-calling-back-from-native-thread-via-jni https://bugs.openjdk.java.net/browse/JDK-8033696

http://docs.oracle.com/javase/7/docs/technotes/guides/jni/jni-14.html#AttachCurrentThreadAsDaemon

About

Example for making C libraries execute callbacks that call java code.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published