JNI with multidimensional Byte Array

Discussion in 'Java' started by abhi147@gmail.com, Oct 5, 2006.

  1. Guest

    Hi all ,

    I am a newbie in JNI . I need to pass a two dimensional byte array
    through JNI .

    The snippet of JNI function is like this :

    JNIEXPORT jstring JNICALL Java_com_test_invokeTest
    (JNIEnv *env, jobject cl, jobjectArray detail)
    {
    int i ,nRows ;
    jobject col;
    char *Details[20];

    nRows = (*env)->GetArrayLength(env, detail);
    for (i = 0; i < nRows; i++)
    {
    col = (*env)->GetObjectArrayElement(env, detail, i);
    Details = (*env)->GetCharArrayElements(env, col, NULL);
    printf("Details[%d] : %s\n",i,Details);
    }

    for(i = 0; i < nRows; i++)
    (*env)->ReleaseCharArrayElements(env, col, Details,NULL);

    return ((*env)-> NewStringUTF(env,str));

    }


    When I am passing a 2-dimensional byte array and executing it without
    any java flags , it works properly most of the time (occasionally it
    was crashing the JVM ) . So I tried using flags "-Xcheck:jni -Xfuture"
    as suggested by Sun . When I used this flags , it is giving me this
    error

    FATAL ERROR in native method: Array element type mismatch in JNI.


    It's crashing just before the call Details =
    (*env)->GetCharArrayElements(env, col, NULL);

    Can anyone tell me what's wrong with this call .. or should I use some
    other call ?

    Thanks a lot of your help !
     
    , Oct 5, 2006
    #1
    1. Advertising

  2. On 4 Oct 2006 21:30:41 -0700, wrote:
    > I am a newbie in JNI . I need to pass a two dimensional byte array
    > through JNI .


    [...]

    > FATAL ERROR in native method: Array element type mismatch in JNI.
    >
    > It's crashing just before the call
    > Details = (*env)->GetCharArrayElements(env, col, NULL);
    >
    > Can anyone tell me what's wrong with this call .. or should I use
    > some other call ?


    There are many problems with your code, not specifically related to
    the place where it crashes. First I strongly suggest that you compile
    with all warnings turned on, then fix the code so that you don't get
    any warnings.

    Here are a few things I can see directly:

    - you say that you have a 2-D byte array, but you seem to have coded
    for a 2-D char array. Which is it? I'll assume you really meant
    byte.

    - col should be declared "jobjectArray".

    - GetCharArrayElements() should be GetByteArrayElements().

    - Details should be array of jbyte*.

    - you shouldn't pass Details directly to printf(), since they
    aren't C strings (consider that they might not be terminated).

    - ReleaseCharArrayElements() should be ReleaseByteArrayElements(), and
    the final argument should be an integer, not a pointer.

    - There is no "str" in context so the code is not compilable as
    posted, and all bets are off regarding the call to NewStringUTF(). I
    can't help you fix the code you didn't post.

    Fix those things, add some error checking (make sure that none of your
    function calls are returning NULL or raising exceptions), then post
    new, compilable code if you're still having problems.

    /gordon

    --
    [ don't email me support questions or followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Oct 5, 2006
    #2
    1. Advertising

  3. Guest

    Hi Gordon ,
    Thanks a lot for your time . I need just one more help

    >
    > Here are a few things I can see directly:
    >
    > - you say that you have a 2-D byte array, but you seem to have coded
    > for a 2-D char array. Which is it? I'll assume you really meant
    > byte.
    >
    > - col should be declared "jobjectArray".
    >
    > - GetCharArrayElements() should be GetByteArrayElements().
    >
    > - Details should be array of jbyte*.
    >



    How can I convert the Details of jbyte* type to char* ?
     
    , Oct 5, 2006
    #3
  4. On 4 Oct 2006 23:58:08 -0700, wrote:
    > How can I convert the Details of jbyte* type to char* ?


    You should be able to cast.

    However if it's a C *string* you want, then casting isn't sufficient,
    you need to terminate the string properly. For that you need to copy
    the data and then add the terminating 0.

    /gordon

    --
    [ don't email me support questions or followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Oct 5, 2006
    #4
  5. Guest

    It's working .. Thanks a lot :)

    Gordon Beaton wrote:
    > On 4 Oct 2006 23:58:08 -0700, wrote:
    > > How can I convert the Details of jbyte* type to char* ?

    >
    > You should be able to cast.
    >
    > However if it's a C *string* you want, then casting isn't sufficient,
    > you need to terminate the string properly. For that you need to copy
    > the data and then add the terminating 0.
    >
    > /gordon
    >
    > --
    > [ don't email me support questions or followups ]
    > g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    , Oct 5, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bharat Bhushan

    Appending byte[] to another byte[] array

    Bharat Bhushan, Aug 5, 2003, in forum: Java
    Replies:
    15
    Views:
    40,406
    Roedy Green
    Aug 5, 2003
  2. static

    JNI byte array to string

    static, Jun 15, 2004, in forum: Java
    Replies:
    13
    Views:
    8,302
    Roedy Green
    Jun 18, 2004
  3. Kirby
    Replies:
    3
    Views:
    668
    Kirby
    Oct 8, 2004
  4. Guest
    Replies:
    2
    Views:
    2,018
    Guest
    Jun 7, 2007
  5. Guest
    Replies:
    1
    Views:
    619
    Alan Johnson
    Jun 6, 2007
Loading...

Share This Page