Best way to throw exceptions in JNI code?

We just code utility methods for each of the types of exceptions we want to throw. Here are some examples: jint throwNoClassDefError( JNIEnv *env, char *message ) { jclass exClass; char *className = “java/lang/NoClassDefFoundError”; exClass = (*env)->FindClass( env, className); if (exClass == NULL) { return throwNoClassDefError( env, className ); } return (*env)->ThrowNew( env, exClass, message … Read more

What is the quantitative overhead of making a JNI call?

Quick profiler test yields: Java class: public class Main { private static native int zero(); private static int testNative() { return Main.zero(); } private static int test() { return 0; } public static void main(String[] args) { testNative(); test(); } static { System.loadLibrary(“foo”); } } C library: #include <jni.h> #include “Main.h” JNIEXPORT int JNICALL Java_Main_zero(JNIEnv … Read more

JNI Hello World Unsatisfied Link Error

The Unsatisfied Link Error can mean many things went wrong. I would use System.loadLibrary(“HelloWorld”); Instead of System.load(); As TwentyMiles suggested. Also, when invoking your program you need to (assuming your DLL is on the same directory as your class files: java -Djava.library.path=. HelloWorld Here’s a simple demo I made that calls a Win32 API function … Read more

Is there a Java library of Unix functions?

I’m aware of two compelling projects: posix for Java (based on JNI) [derived from Jython] jna-posix (based on JNA) [derived from JRuby] Personally I like very much JNA. Take a look at this example of mine, mapping link(2): import com.sun.jna.Library; import com.sun.jna.Native; class Link { private static final C c = (C) Native.loadLibrary(“c”, C.class); private … Read more

Write file to location other than SDcard using Android NDK?

You can always access the directory in which your app is placed. This directory is usually : data/data/<Your_package_name_usually com.android.appName>/files/<your_filename> but better use getFilesDir() to ensure valid filepath with future version changes of android. If you wanna use external storage instead, make sure you place this line of code in your app manifest to gain permission. … Read more

Generating Java interface with SWIG

You can achieve what you’re looking for with SWIG+Java using “Directors”, however it’s not quite as straightforward mapping from the C++ abstract classes onto Java as you might hope. My answer therefore is split into three parts – firstly the simple example of implementing a C++ pure virtual function in Java, secondly an explanation of … Read more

How do I get a list of JNI libraries which are loaded?

[DISCLAIMER: Note that this solution always was hackish. And in most cases won’t work anymore nowadays. Check Benjamins answer for more info] There is a way to determine all currently loaded native libraries if you meant that. Already unloaded libraries can’t be determined. Based on the work of Svetlin Nakov (Extract classes loaded in JVM … Read more

tech