从下用JNI对象获取对象 [英] Get object from an object with JNI in C

查看:125
本文介绍了从下用JNI对象获取对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

下面是Java类的样本;用C我试图让人的学生,但我失败了。但我能够从学生获得的int类型量。

Here is the sample of the class in Java; in C I tried to get the "people" in "Student", but I failed. But 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, "Ltest/jni/bean/Student$People;");

    if (fid == NULL)
    {
        return 0; /* failed to find the field */
    }

    /* Read the instance field s */
    i = (*env)->GetObjectField(env, obj, fid);

    return i;
}

我试图通过人作为一个字段名到方法,但它仍然提供了以下错误:java.lang.NoSuchFieldError:人民

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

推荐答案

作为文件的此处,在 GetFieldID 方法,你不能用L这一点。

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.

例如,如果要指定该参数是一个字符串,你将不得不使用 Ljava /朗/字符串; (最后一个分号是签名的一部分!)。

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!).

有关命名的自定义类,假设它是在包 your.package.name ,你会必须使用 Lyour /包/名/人; 作为一个类型签名

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.

这篇关于从下用JNI对象获取对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆