JNI:native returns an object that contains arrays

Discussion in 'Java' started by theodosisx, Jun 10, 2005.

  1. theodosisx

    theodosisx Guest


    I have a "Response" class in my application, that looks like that:

    class Response
    long[] l_arr = new long[3];
    int[] i_arr = new int[5];

    and I declared a native function that returns a Response object.

    public native Response fillValuesFromC().

    I find the IDs of the arrays (GetFieldID) but I don't know how to
    assign values from C-arrays to them. When it is done, does the
    returning object contains the values?

    Does anyone have an idea or an example?


    theodosisx, Jun 10, 2005
    1. Advertisements

  2. You need more than the IDs, you need references to the arrays
    themselves. Use GetObjectField().

    To actually fill the arrays with values, use SetLongArrayElement() (or
    SetLongArrayRegion()) and SetIntArrayElement() (or
    Most likely.

    Gordon Beaton, Jun 10, 2005
    1. Advertisements

  3. Hi Gordon,

    thanks for your answer. It is done for arrays of primitive types.
    Now, I have an array of Strings in the same class (String[] sNames)
    and I would like to fill this array according to a *char[] array from

    Is it possible? There isn't any SetStringArray... (...) function in JNI.

    thanks in advance

    Theodosis Ekizoglos, Jun 13, 2005
  4. theodosisx

    Sonal Guest


    You need to define a jobjectArray ret

    ret = (jobjectArray) (env)->NewObjectArray(length of out params,

    Then create UTF strings :

    jstring arrayElement = (jstring) env->NewStringUTF(C String);

    Set the above String in the array at the kth position

    env->SetObjectArrayElement(ret,k , arrayElement);

    Hope this helps!

    Sonal, Jun 13, 2005
  5. theodosisx

    theodosisx Guest

    Thank you Sonal,

    now also String[ ] fields are filled from the C code and returned back
    as part of the entire object.

    Thank you very much Gordon, Sonal.

    theodosisx, Jun 13, 2005
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.