Get an object from an object with JNI in C

public class Student { private People people; private Result result; private int amount; } 

Here is a sample class in Java; in C, I tried to get โ€œpeopleโ€ in โ€œStudent,โ€ but I failed. However, I can get int type "amount" from "Student".

 jobject getObjectFromObject(JNIEnv *env, jobject obj, const char * fieldName) { jfieldID fid; /* store the field ID */ jobject i; /* Get a reference to obj class */ jclass cls = (*env)->GetObjectClass(env, obj); /* Look for the instance field s in cls */ fid = (*env)->GetFieldID(env, cls, fieldName, "L"); if (fid == NULL) { return 0; /* failed to find the field */ } /* Read the instance field s */ i = (*env)->GetObjectField(env, obj, fid); return i; } 

I am trying to pass "people" as fieldName to a method, but it still gives the following error: "java.lang.NoSuchFieldError: people"

+4
source share
1 answer

As described here , in the GetFieldID method you cannot use only โ€œLโ€ as the type signature, you must specify the class name after that.

For example, if you want to indicate that the argument is String , you will need to use Ljava/lang/String; (the end semicolon is part of the signature!).

For your custom class called People , assuming it in the package your.package.name , you will need to use Lyour/package/name/People; like a signature type.

+8
source

All Articles