How do I access return value of a Java method returning java.lang.String from C++ in JNI?

前端 未结 4 1281
没有蜡笔的小新
没有蜡笔的小新 2021-01-12 15:02

I am trying to pass back a string from a Java method called from C++. I am not able to find out what JNI function should I call to access the method and be returned a jstrin

4条回答
  •  一整个雨季
    2021-01-12 15:45

    You should have

    cls = env->FindClass("ClassifierWrapper"); 
    

    Then you need to invoke the constructor to get a new object:

    jmethodID classifierConstructor = env->GetMethodID(cls,"", "()V"); 
    if (classifierConstructor == NULL) {
      return NULL; /* exception thrown */
    }
    
    jobject classifierObj = env->NewObject( cls, classifierConstructor);
    

    You are getting static method (even though the method name is wrong). But you need to get the instance method since getString() is not static.

    jmethodID getStringMethod = env->GetMethodID(cls, "getString", "()Ljava/lang/String;"); 
    

    Now invoke the method:

    rv = env->CallObjectMethod(classifierObj, getStringMethod, 0); 
    const char *strReturn = env->GetStringUTFChars(env, rv, 0);
    

提交回复
热议问题