AndroidDev
AndroidDev

Reputation: 2647

JNI Pass Char* 2D array to JAVA Code

I want to pass the following pointer array through the JNI layer from C code

char *result[MAXTEST][MAXRESPONSE] = {
    { "12", "12", "" },
    { "8",  "3",  "" },
    { "29", "70", "" },
    { "5",  "2",  "" },      
    { "42", "42", "" }
};

In java code I have written the following declaration

public static native String[][] getResult();

I am confused how to pass that array through JNI layer to Java code??? Following is the JNI layer description

JNIEXPORT jobjectArray JNICALL Java_com_example_CheckResult_getResult
  (JNIEnv *env, jclass thiz) {
Confused over here ????
}

Upvotes: 3

Views: 1854

Answers (1)

AndroidDev
AndroidDev

Reputation: 2647

Finally after hours working on jop's shared link, I could solve my problem. The code goes as below:

JNIEXPORT jobjectArray JNICALL Java_com_example_CheckResult_getResult(JNIEnv *env, jclass thiz) {
    jboolean flag = JNI_TRUE;
    jclass stringClass = (*env)->FindClass(env, "java/lang/String");
    jobjectArray row;
    jobjectArray rows;

    jsize i, j;
    for(i=0; i<5; i++) {
        row = (*env)->NewObjectArray(env, MAXRESPONSE, stringClass, 0);
        for(j=0; j<3; j++) {
            (*env)->SetObjectArrayElement(env, row, j, (*env)->NewStringUTF(env, userResponse[i][j]));
        }

        if(flag == JNI_TRUE) {
            flag = JNI_FALSE;
            rows = (*env)->NewObjectArray(env, MAXTEST, (*env)->GetObjectClass(env, row), 0);
        }

        (*env)->SetObjectArrayElement(env, rows, i, row);
    }

    return rows;
}

Upvotes: 2

Related Questions