1

so I have String resultNative[] in Java and char result[][] in C++, how can I pass array from java to c++ using JNI? I have tried the following code but error. thank you.

JNIEXPORT void JNICALL Java_com_qualcomm_QCARSamples_ImageTargets_GetTargetData_returnResult(JNIEnv *env, jobject obj) {
int size = env->GetArrayLength(resultNative);

for (int i=0; i < size; ++i)
{
    jstring string = env->GetObjectArrayElement(resultNative, i);
    result[i] = env->GetStringUTFChars(string, i);
    env->ReleaseStringUTFChars(string, resultNative);
    env->DeleteLocalRef(string);
}

}

1
  • 1
    It might be useful if you would also post the actual error you get. Commented Jan 11, 2014 at 15:29

2 Answers 2

1

I think you should make a copy of what GetStringUtf returns before releasing it. Otherwise you store a pointer to a freed memory and perhaps access it later on. Using std:string is the right way to do it in c++, but you could use strdup if you really insist.

If result is an array of std:string, this is as simple as result[i] = std:string(env->GetStringUTFChars(string, i));

Sign up to request clarification or add additional context in comments.

1 Comment

hmm. do you have an example of how to do this properly? maybe the way i'm doing this is wrong.. i'm new to this and not so familiar with C++ :(
0

Your code is missing a parameter. Also, you're releasing the string before you even use it. Assigning it to result[i] just makes a shallow copy (copy of the pointer, not the contents). After releasing it, result[i] becomes invalid.

JNIEXPORT void JNICALL Java_com_qualcomm_QCARSamples_ImageTargets_GetTargetData_returnResult(JNIEnv *env, jobject obj, jobject stringArray)
{
    int length = env->GetArrayLength(stringArray);

    for (int i = 0; i < length; ++i)
    {
        jstring jstr = (jstring) env->GetObjectArrayElement(stringArray, i);
        const char* cpp_string = env->GetStringUTFChars(jstr, 0);

        std::cout<<cpp_string<<"\n";

        env->ReleaseStringUTFChars(jstr, cpp_string);
        env->DeleteLocalRef(jstr);
    }
}

OR if you want to save the string then:

JNIEXPORT void JNICALL Java_com_qualcomm_QCARSamples_ImageTargets_GetTargetData_returnResult(JNIEnv *env, jobject obj, jobject stringArray)
{
    int length = env->GetArrayLength(stringArray);

    std::vector<std::string> array_of_str(length);

    for (int i = 0; i < length; ++i)
    {
        jstring jstr = (jstring) env->GetObjectArrayElement(stringArray, i);
        const char* cpp_string = env->GetStringUTFChars(jstr, 0);

        array_of_str.emplace_back(cpp_string);

        env->ReleaseStringUTFChars(jstr, cpp_string);
        env->DeleteLocalRef(jstr);
    }
}

1 Comment

thank you, umm.. i think i'm wrong with the JNI syntax, can you please help me? stackoverflow.com/questions/21065390/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.