0

In Java, a multiple-dimension array of non-primitive objects, e.g. Integer[][] arr, is defined. How should one access the array arr in a C program via JNI?

public class Foo {
    public static Integer[][] arr = {{0}, {1, 2}, {3, 4, 5}};
}
2
  • 2
    int a[][3] = {{0}, {1, 2}, {3, 4, 5}}; Give it a try!! Commented Oct 10, 2013 at 19:26
  • Not translate: By JNI, I need to access the java data from a c program. Commented Oct 10, 2013 at 19:36

2 Answers 2

8

First, get the field ID:

jclass clazz = (*env)->FindClass(env, "fully/qualified/package/Foo");
jfieldID field = (*env)->GetFieldID(env, clazz, "arr", "[[Ljava/lang/Integer;" );

Then you'll need to use this to get the actual field. Supposing you have a jobject of type Foo called fooObj:

jobject arrObj = (*env)->GetObjectField(env, fooObj, field);

arr can be cast into a jObjectArray, and you can manipulate the array using the jni array functions. Documentation can be found here.

Since you have a 2D array of Integer objects, you will have to go through the usual means to get the primitive type from the Integer class.

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

2 Comments

Thank you, krsteeve. Now, I can get the size of each row, but I encountered problem when casting jobject to jint, for example, 2 => 22164352.
You have a 2D array of Integer objects, they are not int's, and you can't just cast them to jint. You'll have to either change your array to the primitive type, or go through the usual means to get the primitive type from the Integer class
1

At present, it is impossible to directly transfer a multiple-dimension array of some non-primitive objects from a Java program to a C program.

A solution to this problem is to make a primitive version of the non-primitive multiple-dimension array, and to transfer the primitive multiple-dimension array from Java to C. Anyway, such a multiple-dimension array is transferred as a jobjectArray natively.

The outline of working on a 2-dimension array of integer numbers is as follows:

  1. make a primitive version of the non-primitive multiple-dimension array in Java;
  2. get the array as jobjectArray in native C program;
  3. use GetObjectArrayElement to iterate each row;
  4. use GetIntArrayElements function to iterate each cell, e.g. jint *val = (*env)->GetIntArrayElements(env, row, NULL);.

3 Comments

You say it's impossible, but then you proceed to demonstrate how to do it. I'm not following you. Do you mean it's impossible to cast a Java multidimensional array into a contiguous block of array elements in C? Yes, that's impossible.
I mean `directly transferring' is impossible :-)
Is is possible, they'll just be jobjects... then you'll have to get the int values from the Integer class as you would in Java, using jni

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.