11

Good day, I am trying to copy a Java string array to C++ array using JNI. I have tried this but does not seem to be working.

char *myarray;

JNIEXPORT void JNICALL
Java_com_Example_accessArray(JNIEnv *env, jobject obj, jobjectArray stringArrays){
      int size;
         size = env->GetArrayLength(stringArrays);
    myarray = env->GetCharArrayRegion(stringArrays, 0, size, null);

}

Does the myarray hold the same values in the Java array passed? Or how can I copy the values of the java String array passed through JNI to a C++ array, so the array in C++ holds the same vale? Thanks in advance.

1
  • Which character set/encoding (aka "code page") do you want your strings in? Unicode/UTF-16, Unicode/UTF-8, Windows-1252 (but probably not modified UTF-8), ...? Java strings are Unicode, so if you want to convert to another character set, what do you want to do if the target character set is missing a character? Use '?' or another substitute, throw an exception, ...? Commented Oct 26, 2013 at 0:16

2 Answers 2

11

You pass an array of objects so you cannot just get chars from this array. A way would be like this:

int size = env->GetArrayLength(stringArrays);

for (int i=0; i < size; ++i) 
{
    jstring string = env->GetObjectArrayElement(stringArrays, i);
    const char* mayarray = env->GetStringUTFChars(string, 0);
    .... do some work or copy it to a c++ array of char*....
    env->ReleaseStringUTFChars(string, myarray);
    env->DeleteLocalRef(string);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Good answer, but you should call DeleteLocalRef(string) at the end of the loop to avoid creating an unbounded number of object references. Also, it should be ReleaseStringUTFChars(string, myarray).
@pburka: You are right with both. Although if the number of elements in the array is small it makes not much of a difference, but I does not hurt either. And good catch for the ReleaseString call. Usually I use c when using jni and then the last param is the iscopy.
8

This is the code that I use to convert a Java string array String[] to a C++ vector<string>:

void Java_com_domain_project_activity_stringArrToStringVector(JNIEnv *env, jobject obj, jobjectArray jstringArr) {
        vector<string> stringVec;

        // Get length
        int len = env->GetArrayLength(jstringArr);

        for (int i=0; i<len; i++) {
            // Cast array element to string
            jstring jstr = (jstring) (env->GetObjectArrayElement(jstringArr, i));

            // Convert Java string to std::string
            const jsize strLen = env->GetStringUTFLength(jstr);
            const char *charBuffer = env->GetStringUTFChars(jstr, (jboolean *) 0);
            string str(charBuffer, strLen);

            // Push back string to vector
            stringVec.push_back(str);

            // Release memory
            env->ReleaseStringUTFChars(jstr, charBuffer);
            env->DeleteLocalRef(jstr);
        }
}

And on the Java side the function will look like the following:

void stringArrToStringVector(String[] jstringArr);

And finally, as a side note, to convert from a Java string list to string array (which you will pass to your JNI function), you can do the following:

List<String> strList = new ArrayList<>();
// Populate strList
String[] strArr = new String[strList.size()];
strList.toArray(strArr);

Comments

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.