To implement a C callback to java code using JNI, I followed the advice from here and here and adapted my native method implementation to store a reference to the objects I need for the callbacks later:
JavaVM * g_vm;
jobject g_obj;
jmethodID g_mid;
//More code here
JNIEXPORT jboolean JNICALL
Java_a_B_initialize(JNIEnv * env, jobject obj)
{
g_obj = env->NewGlobalRef(obj);
jclass g_clazz = env->GetObjectClass(g_obj);
if (g_clazz == NULL) {
printf("Failed to find class");
}
g_mid = env->GetMethodID(g_clazz, "callback", "(I)V");
if (g_mid == NULL) {
printf("Unable to get method ref");
}
However, this does not compile and I get the error messages:
Left of '-> NewGlobalRef' must point to struct/union
Left of '-> GetObjectClass' must point to struct/union
Left of '-> GtMethodID' must point to struct/union
I do not understand these error message. What's wrong with my code?