2

I'm struggling with creating a 2d array of my custom object type ShareStruct:

jobjectArray ret ;
jobjectArray ins ;
jobjectArray outs;

jclass myClass = (*env)->FindClass(env,"org/apache/s4/core/ShareStruct");
if (myClass==NULL) fprintf(stderr, "Class ShareStruct not found");

jclass myClassArray = (*env)->FindClass(env, "[Lorg/apache/s4/core/ShareStruct");
if (myClassArray==NULL) fprintf(stderr, "Class ShareStruct[] not found");

ins = (*env)->NewObjectArray(env, in, myClass, NULL);
outs = (*env)->NewObjectArray(env, out, myClass, NULL);
ret = (*env)->NewObjectArray(env, 2, myClassArray, NULL);

The first class loading works (the ShareStruct is fine), but the other one (trying to load a ShareStruct[] class) doesn't. I've tried both with and without the L but no luck. Any ideas? I'm new with JNI.

Thanks!

user1018513
  • 1,682
  • 1
  • 20
  • 42

3 Answers3

5

This jclass myClassArray = (*env)->FindClass(env, "[Lorg/apache/s4/core/ShareStruct"); is wrong. To create the array do something like

 ret = (*env)->NewObjectArray(env,sizeOfArray,myClass,NULL);  
(*env)->SetObjectArrayElement( env, ret,index, sharedStructObj);  

Here sharedStructObj will have to be created by newObject.
Section 3.3.5 of JNI programmer's guide has a good related example

This is also nice Create, populate and return 2D String array from native code (JNI/NDK)

EDIT based on comment

in = (*env)->NewObjectArray(env,sizeOfArray,myClass,NULL);
out = (*env)->NewObjectArray(env,sizeOfArray,myClass,NULL);
ret= (*env)->NewObjectArray(env,sizeOfArray,myClass,NULL);
(*env)->SetObjectArrayElement( env, ret,0, in); 
(*env)->SetObjectArrayElement( env, ret,1, out); 
Community
  • 1
  • 1
bobby
  • 2,629
  • 5
  • 30
  • 56
  • 1
    But I want to create an array of ShareStruct[], so the java equivalent would be ShareStruct[][]. I don't see how this code does that :( Effectively, I want to do : ShareStruct[] in; ShareStruct[] out; ShareStruct[][] ret ; ret[0] = in ; ret[1] = out ; – user1018513 Jul 27 '12 at 10:56
  • 1
    This is a good answer, but "Section 3.3.5 of JNI programmer's guide has a good related example", link is dead as of 2016-05-18. – CloudyTrees May 18 '16 at 21:34
  • 1
    link to JNI Programmer's guide (Section 3.3.5 has the example mentioned by @CloudyTrees): http://barbie.uta.edu/~jli/Resources/Resource%20Provisoning&Performance%20Evaluation/85.pdf – I L Aug 15 '19 at 16:08
1

You have to use an object array for the outer array:

jclass myClassArray = (*env)->FindClass(env, "[Ljava/lang/Object;");

in a similar case with a 2D String array worked for me. Please also recognize the trailing semicolon in the string.

miro
  • 61
  • 3
1

I do not know if this question is still relevant, but I think you simply forgot the semicolon at the end of your array class specification:

jclass myClassArray = (*env)->FindClass(env, "[Lorg/apache/s4/core/ShareStruct;");
Albert Lazaro de Lara
  • 2,540
  • 6
  • 26
  • 41
Eric
  • 11
  • 1