JNI如何访问Java对象(整数)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2630529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 10:05:33  来源:igfitidea点击:

JNI how to access Java Object (Integer)

javac++java-native-interface

提问by user271290

I have a JNI method to access java method which returns an Integer object. I do not want to return the primitive int type because this code will be modified to handle Generic objects. The following is what I have. I am not able to get the value of the Integer that I pass. The output at C++ side is something like

我有一个 JNI 方法来访问返回一个 Integer 对象的 java 方法。我不想返回原始 int 类型,因为将修改此代码以处理 Generic 对象。以下是我所拥有的。我无法获得我传递的 Integer 的值。C++ 端的输出类似于

value = 0x4016f3d0

How can I get the actual value of Integer object that I pass at C++ end?

如何获得我在 C++ 端传递的 Integer 对象的实际值?

Please help.

请帮忙。

Thanks,

谢谢,

-H

-H

GenericPeer.cpp

通用对等文件

JNIEXPORT void JNICALL Java_GenericPeer_print (JNIEnv *jenv, jclass jcls, jobject data){
 jclass peerCls = jenv->GetObjectClass(data);
 jmethodID mGetValue = jenv->GetMethodID(peerCls, "getValue","()Ljava/lang/Integer;");
 if(mGetValue == NULL){
   return (-1);
 } 
 jobject value = jenv->CallObjectMethod(data, mGetValue);
 cout<<"value = "<<value<<endl;

}

GenericPeer.java

通用Peer.java

public class GenericPeer {
 public static native void print(Data d);
 static {
  System.load("/home/usr/workspace/GenericJni/src/libGenericJni.so");
 }
}

Data.java

数据.java

public class Data {
 private Integer value;
 pubilc Data(Integer v){ 
  this.value = v;
 }
 public Integer getValue() { return value; }
    public void setValue(Integer value) {
 this.value = value;
 }
}

Test.java (Main class)

Test.java(主类)

public class Test {
 public static void main(String[] args){
       Integer i = new Integer(1);
  Data d = new Data(i);
  GenericPeer.print(d);
      }
}

采纳答案by jarnbjo

You have to invoke the intValuemethod on the Integer instance to get its primitive value. Use FindClassinstead of GetObjectClass(as in your code) to get a reference to the class java.lang.Integer and then GetMethodIDand CallObjectMethodto actually invoke the intValuemethod.

您必须调用intValueInteger 实例上的方法以获取其原始值。使用FindClass替代GetObjectClass(如在你的代码),以获得对类java.lang.Integer中的引用,然后GetMethodIDCallObjectMethod实际调用的intValue方法。

回答by user271290

Thanks Jarnbjo,

谢谢Jarnbjo,

It works now! This is what I have:

它现在有效!这就是我所拥有的:

    JNIEXPORT jint JNICALL Java_GenericPeer_print (JNIEnv *jenv, jclass jcls, jobject data){
      jclass peerCls = jenv->GetObjectClass(data);

     jmethodID mGetValue = jenv->GetMethodID(peerCls, "getValue","()Ljava/lang/Integer;");
     if (mGetValue == NULL){
       return(-1);
     }

     jobject value = jenv->CallObjectMethod(data, mGetValue);
     if(value == NULL){
      cout<<"jobject value = NULL"<<endl;
      return(-1);
     }

    //getValue()

     jclass cls = jenv->FindClass("java/lang/Integer");
     if(cls == NULL){
       outFile<<"cannot find FindClass(java/lang/Integer)"<<endl;
     }
       jmethodID getVal = jenv->GetMethodID(cls, "intValue", "()I");
       if(getVal == NULL){
         outFile<<"Couldnot find Int getValue()"<<endl;
       }
       int i = jenv->CallIntMethod(value, getVal);
}