4

I have a Java instance method which returns a String and I'm calling this method through JNI in C++. I have written the following code:

const char *DiagLayerContainer_getDESC(JNIEnv *env, jobject diagLayer) {
    jclass diagLayerClass = env->FindClass(PARSER_CLASS);
    jmethodID getDESCDiagLayerMethodID = env->GetMethodID(diagLayerClass, "getDESCDiagLayer", "(Ljava/lang/Object;)Ljava/lang/String;");
    jstring returnString = (jstring) env->CallObjectMethod(diagLayer, getDESCDiagLayerMethodID);
    return env->GetStringUTFChars(returnString, JNI_FALSE);
}

How do I get the string and convert it to a const char *?

My program crashes on the last line with access violation to 0x00000000. returnString is not NULL.

Salvatore
  • 1,145
  • 3
  • 21
  • 42
  • 1
    Run your code in a debugger, and examine the state of the process right at the point of the crash. – NPE Mar 07 '13 at 10:19
  • It crashes here (jni.h): const char* GetStringUTFChars(jstring str, jboolean *isCopy) { return functions->GetStringUTFChars(this,str,isCopy); } without any chance to go deeper with the debugger – Salvatore Mar 07 '13 at 10:21

1 Answers1

7

According to GetStringUTFChars, the last parameter is a pointer to jboolean.

Change

return env->GetStringUTFChars(returnString, JNI_FALSE);

to

return env->GetStringUTFChars(returnString, NULL);

Or better yet, return a std::string

std::string DiagLayerContainer_getDESC(...) {
    ...
    const char *js = env->GetStringUTFChars(returnString, NULL);
    std::string cs(js);
    env->ReleaseStringUTFChars(returnString, js);
    return cs;
}

I've built a similar simple example and the code as is, seems fine so far.

Although, there are two possible error sources.

The first one is the method signature. Try "()Ljava/lang/String;" instead of "(Ljava/lang/Object;)Ljava/lang/String;".

The second one is in the java source itself. If the java method returns a null string, CallObjectMethod() will return a NULL jstring and GetStringUTFChars() fails.

Add a

if (returnString == NULL)
    return NULL;

after CallObjectMethod().

So look into the java source and see, whether the method getDESCDiagLayer() might return a null string.

Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
  • 1
    If `isCopy` is false and `returnString` goes out of scope (i.e. through an explicit or implicit `PopLocalFrame`), you'll have a dangling pointer. You should `strdup()` the result of `GetStringUTFChars`, return that, and call `ReleaseStringUTFChars`. Later, dispose of the copy with `free()`, as usual in native code. – jop Mar 07 '13 at 11:32
  • strdup() is replaced with std::string(), which will copy new allocated string itself. – Pihhan Mar 07 '13 at 13:13
  • How can I print returnString in Logcat? – K.Sopheak Oct 14 '16 at 03:39
  • What do you mean by Logcat? Android's Logcat? – Olaf Dietsche Oct 14 '16 at 08:21
  • @K.Sopheak, you can use [__android_log_write](https://developer.android.com/ndk/reference/group/logging#group___logging_1ga32a7173b092ec978b50490bd12ee523b) – Ieshaan Saxena Feb 21 '22 at 13:26