Slide 79
Slide 79 text
extern "C"
JNIEXPORT void JNICALL Java_ClassName_MethodName
(JNIEnv *env, jobject obj, jstring javaString)
{
//Get the native string from javaString
const char *nativeString = env->GetStringUTFChars(javaString, 0);
std::cout << nativeString << std::endl;
//DON'T FORGET THIS LINE!!!
env->ReleaseStringUTFChars(javaString, nativeString);
}
(Wikipedia)
The problem is that the JNI is quite complicated to write -- this is a simple echo
function that I pinched from Wikipedia. And for just a simple function like this,
there’s already three really complicated things you have to do:
You have to get this method name exactly right depending on what class
and method you’re implementing.
Getting variables out of Java-land has a really complex API
And you have to manually manage your Java objects within C.
For a codebase of any degree of complexity, it’s basically impossible to get this right
and maintain it. For example, if you do any refactoring of Java code, your C method
names might be wrong.