Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get object from an object with JNI in C

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

Here is the sample of the class in Java; in C, I tried to get the "people" in "Student", but I failed. However, I am able to 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's 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 a fieldName into the method, but it still gives the following error: "java.lang.NoSuchFieldError: people"

like image 529
user1151874 Avatar asked Feb 17 '23 22:02

user1151874


1 Answers

As documented here, in the GetFieldID method you can't use "L" alone as a type signature, you have to specify the class name after that.

For example if you want to specify that the argument is a String, you'll have to use Ljava/lang/String; (The final semicolon is part of the signature!).

For your custom class named People, supposing it's in the package your.package.name, you'll have to use Lyour/package/name/People; as a type signature.

like image 115
mbrenon Avatar answered Feb 26 '23 22:02

mbrenon