java-native-interface
JNI – How to callback from C++ or C to Java?
There are many valid ways to callback into Java from C/C++. I’m going to show you a technique using C (easy to adjust env for C++) that makes it fairly easy to pass data from native code to Java code. This example passes strings ( easy to modify for any data type ). In native …
Keeping a global reference to the JNIEnv environment
You cannot cache the JNIEnv pointer. Read about it here: The JNI interface pointer (JNIEnv) is valid only in the current thread. Should another thread need to access the Java VM, it must first call AttachCurrentThread() to attach itself to the VM and obtain a JNI interface pointer. Once attached to the VM, a native …
Multiple directories in -Djava.library.path
On Linux, use colon : as separator (as you will do with the classpath option) as in: -Djava.library.path=/opt/hdf-java/build/bin:/opt/opencv-2.4.10/build/lib
JNI vs. JNA performance
JNA is much slower than JNI, but much easier. If performance is not an issue use JNA. Using direct buffers have the advantage that the most critical operations don’t use JNI or JNA and are thus faster. They use intrinsic when means they get turned into single machine code instructions. If Java code is significantly …
JNI: How can i check if jobject is a null object in native c code
Since the objects in Java and C code actually use the same memory locations (the object passed to the native code is the same memory reference in both worlds), a simple if (someJObject == NULL) {} in the C code should be just fine I guess. I haven’t tested it though 🙂
How to compile dynamic library for a JNI application on linux?
Native library can be loaded by loadLibrary with a valid name. By example, libXXXX.so for linux family, your hellolib.so should rename to libhello.so. By the way, I develop java with jni, I will separate the implementation and native interface (.c or .cpp). static { System.loadLibrary(“hello”); // will load libhello.so } The implementation header(HelloImpl.h): #ifndef _HELLO_IMPL_H …
Should Java finalizer really be avoided also for native peer objects lifecycle management?
finalize and other approaches that use GC knowledge of objects lifetime have a couple of nuances: visibility: do you guarantee that all the writes methods of object o made are visible to the finalizer (i.e., there is a happens-before relationship between the last action on object o and the code performing finalization)? reachability: how do …
How to call C++ from Java?
While I’ve used JNI-C++ bridging in the past (only a little though) – it can be a bit ugly. You might want to consider using SWIG to help you generate all the messy boiler plate code.
How to use NDK? Starting with ‘hello world’ app
There are samples in the android-ndk-r5 folder + there is a nice step-by-step tutorial: https://developer.android.com/ndk/samples/index.html No excuses, dude. You just have to read (take a look at the Exploring the hello-jni Sample section).