user1759789 user1759789 - 2 years ago 85
C Question

Accessing field using JNI

I have a java class that has two fields - one is a private final double and the other is a private Map; I have added a public getter method for the Map. I am accessing them from my C code using JNI. I am having problems getting the Map via the field but have no problem getting it through the method:

// The following lines of code work just fine
jclass jCls = (*env)->GetObjectClass(env,object);
jfieldID dblFldId = (*env)->GetFieldID(env,jCls,"nameOfDoubleVariable","D");
jdouble dblVar = (*env)->GetDoubleField(env, object, dblFldId);

// These lines don't work though
jfieldID mapId = (*env)->GetFieldID(env,jCls,"nameOfMapVariable","()Ljava/util/Map;");
jobject mapVar = (*env)->GetObjectField(env,object,mapId);

But if I replace the two lines that don't work with the following (essentially I am getting the map via the method instead of directly via the field) it does work:

jmethodID m_GetMap = (*env)->GetMethodID(env,jCls,"getMap","()Ljava/util/Map;");
jobject mapVar = (*env)->CallObjectMethod(env,object,m_GetMap);

Can anyone tell me why the method call works but not getting it from the field - I am sure I am doing something wrong!

Answer Source

The error is that you try to get a field with a function signature. Try with this:

jfieldID mapId = (*env)->GetFieldID(env,jCls,"nameOfMapVariable","Ljava/util/Map;");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download