Search code examples
java-native-interface

JNI Calling Java Method With Array Parameter


I am trying to call a java method from cpp. I seem to have no problem using strings, int, etc. One problem I am having those is passing an int array parameter over. Can someone tell me what I did wrong? I apologize if it is a very small error and I just totally missed it.

JNIEXPORT void JNICALL
Java_basket_menu_MenusActivity_submitInfo(JNIEnv *, jclass){
    int placement[2] = { 5, 4 };

    jclass cls = env->FindClass("basket/menu/MenusActivity");
    jmethodID mid2 = env->GetStaticMethodID(cls, "PlaceMe", "([I)V");
    env->CallStaticVoidMethod(cls, mid2, placement); 
}

Solution

  • You need to create a jintArray and copy the contents of placement to it:

        jintArray arr = env->NewIntArray(2);
        env->SetIntArrayRegion(arr, 0, 2, placement);
        env->CallStaticVoidMethod(cls, mid2, arr); 
    

    Refer to the documentation for more information about these functions.