Alan L
Alan L

Reputation: 133

How to read an array of objects passed into JNI from Java

This might sound like a newbie question but I would like to know the proper way to read and iterative through an array of objects passed into JNI c++ from java.

For examples, the object has a signature of:

public class Node{
     public String name;
     public long uniqueId;
     public int rank;
}

And I pass an array of ten node objects into a native method:

nativeParse(nodes);

I would like to iterative through the objects and create new c++ objects from the data so that my c++ code can use them.

Any help would be appreciated!

Upvotes: 4

Views: 11081

Answers (1)

Juvanis
Juvanis

Reputation: 25950

Assuming is newNodes[] is your C++ array of objects, you can try something like this:

  JNIEXPORT void JNICALL nativeParse(JNIEnv *env, jint size, jobjectArray nodes)
  {
     for(int i=0; i < size; i++)
         newNodes[i]= (jobject) env->GetObjectArrayElement(nodes, i);
  }

Upvotes: 7

Related Questions