Get object from an object with JNI in C

19,533

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.

Share:
19,533
user1151874
Author by

user1151874

Updated on June 04, 2022

Comments

  • user1151874
    user1151874 almost 2 years
    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"

    • user1151874
      user1151874 about 11 years
      i have fix the type signature by passing the package name.But still unable to fix my issue
  • user1151874
    user1151874 about 11 years
    fid = (*env)->GetFieldID(env, cls, fieldName, "LCalculateResult/src/student/jni/Student;"); still unable to find, should i put the inner class"People" or "Student", but i tried both also failed. I just realise both project is different path, how it able to locate it?
  • mbrenon
    mbrenon about 11 years
    You have to use the package name, not the path.
  • mbrenon
    mbrenon about 11 years
    Look into your People.java file, it must start with something like package your.package.name;. Convert the dots to "/" and you'll have a valid type signature. If People is an inner class of Student, I think you'll have to separate the "external" class from the inner class using a $ : use Lyour/package/name/Student$People;
  • user1151874
    user1151874 about 11 years
    package test.jni.bean; code these is my package name, so i converted into fid = (*env)->GetFieldID(env, cls, fieldName, "Ltest/jni/bean/Student$People;"); code but still java.lang.NoSuchFieldError: people
  • user1151874
    user1151874 about 11 years
    anyways i fixed my issue,codefid = (*env)->GetFieldID(env, cls, fieldName, "Ltest/jni/bean/People;"); code just give the package of the class, forget the outer class of "Student"