Aykut Aykut - 1 year ago 207
Java Question

passing string array from java to C with JNI

I have a string array like

and I am trying to send this array to C with using JNI. I could not find any clear solution for this. I have tried to take this string as a
but no success.

Is there a way to do this?

Answer Source

You can write a simple function that takes a jobjectArray object, cast each one to jstring and then call GetStringUTFChars on it.

Like this:

void MyJNIFunction(JNIEnv *env, jobject object, jobjectArray stringArray) {

    int stringCount = env->GetArrayLength(stringArray);

    for (int i=0; i<stringCount; i++) {
        jstring string = (jstring) (env->GetObjectArrayElement(stringArray, i));
        const char *rawString = env->GetStringUTFChars(string, 0);
        // Don't forget to call `ReleaseStringUTFChars` when you're done.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download