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

JNI如何访问Java对象(整数),java,c++,java-native-interface,Java,C++,Java Native Interface,我有一个JNI方法来访问返回整数对象的java方法。我不想返回原语int类型,因为此代码将被修改以处理泛型对象。以下是我所拥有的。我无法获得传递的整数的值。C++端的输出类似于 value = 0x4016f3d0 我如何获得C++结尾的整数对象的实际值? 请帮忙 谢谢 -H GenericPeer.cpp Data.java java(主类) 您必须在Integer实例上调用intValue方法来获取其原语值。使用FindClass而不是GetObjectClass(如在代码中)获取对类ja

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

value = 0x4016f3d0

我如何获得C++结尾的整数对象的实际值? 请帮忙

谢谢

-H

GenericPeer.cpp

Data.java

java(主类)


您必须在Integer实例上调用
intValue
方法来获取其原语值。使用
FindClass
而不是
GetObjectClass
(如在代码中)获取对类java.lang.Integer的引用,然后使用
GetMethodID
CallObjectMethod
实际调用
intValue
方法。

谢谢Jarnbjo

现在可以了!这就是我所拥有的:

    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);
}   
JNIEXPORT jint JNICALL Java_GenericPeer_print(JNIEnv*jenv,jclass jcls,jobject数据){
jclass peerCls=jenv->GetObjectClass(数据);
jmethodID mGetValue=jenv->GetMethodID(peerCls,“getValue”,“()Ljava/lang/Integer;”;
if(mGetValue==NULL){
返回(-1);
}
jobject value=jenv->CallObjectMethod(数据,mGetValue);
如果(值==NULL){

coutWhy
Ljava/lang/Integer;
,而不是
Ijava/lang/Integer;
?@IgorGanapolsky,因为它是传递Java类型“Lpackage\u name/class\u name;”的标准JNI方法我和长时间的工作没有关系Int@Flot2011区别是什么这应该是公认的答案。这里有实际的代码,不像上面的复制粘贴说明。
public class GenericPeer {
 public static native void print(Data d);
 static {
  System.load("/home/usr/workspace/GenericJni/src/libGenericJni.so");
 }
}
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;
 }
}
public class Test {
 public static void main(String[] args){
       Integer i = new Integer(1);
  Data d = new Data(i);
  GenericPeer.print(d);
      }
}
    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);
}