-1

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?

Community
  • 1
  • 1
RoflcoptrException
  • 51,941
  • 35
  • 152
  • 200

2 Answers2

3

You are attempting to use the C++ interface. You've tagged the question [C] so I assume you are using C.

In C, do it this way:

(*env)->NewGlobalRef(env, obj)
Tom Blodget
  • 20,260
  • 3
  • 39
  • 72
1

You are missing the JNI include:

#include <jni.h>
vz0
  • 32,345
  • 7
  • 44
  • 77