1
votes

I have a method in my Java class which returns a string . method signature currently ()Ljava/lang/String; I could create the class and method id with JVM in my native call. Have done GetStaticMethodID etc... all are fine.

How to invoke this method? for example: env->CallIntMethod() env->CallCharMethod() .... which option to get a string back?

In C++ , I need to copy this string value retured by this Java method. Note: I can change the signature of the method in the Jar if needed. Or can add another method to wrap the other one.

1

1 Answers

6
votes

A method that returns Java String should be called as

jstring js = static_cast<jstring>(env->CallObjectMethod(jobj, mId));

Now you can use

const char *cstr = env->GetStringUTFChars(js, nullptr);

Don't forget to release the string after use:

env->ReleaseStringUTFChars(js, cstr);

You may find it necessary to to release the Java local reference,

env->FreeLocalReference(js);