Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > JNI with multidimensional Byte Array

Reply
Thread Tools

JNI with multidimensional Byte Array

 
 
abhi147@gmail.com
Guest
Posts: n/a
 
      10-05-2006
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[i] = (*env)->GetCharArrayElements(env, col, NULL);
printf("Details[%d] : %s\n",i,Details[i]);
}

for(i = 0; i < nRows; i++)
(*env)->ReleaseCharArrayElements(env, col, Details[i],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[i] =
(*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 !

 
Reply With Quote
 
 
 
 
Gordon Beaton
Guest
Posts: n/a
 
      10-05-2006
On 4 Oct 2006 21:30:41 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) 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[i] = (*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[i] 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
 
Reply With Quote
 
 
 
 
abhi147@gmail.com
Guest
Posts: n/a
 
      10-05-2006
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* ?

 
Reply With Quote
 
Gordon Beaton
Guest
Posts: n/a
 
      10-05-2006
On 4 Oct 2006 23:58:08 -0700, (E-Mail Removed) 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
 
Reply With Quote
 
abhi147@gmail.com
Guest
Posts: n/a
 
      10-05-2006
It's working .. Thanks a lot

Gordon Beaton wrote:
> On 4 Oct 2006 23:58:08 -0700, (E-Mail Removed) 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
JNI/c++ - passing imagefile as a byte array to native library Java 2 06-07-2007 09:47 AM
JNI/c++ - passing imagefile as a byte array to native library C++ 1 06-06-2007 10:28 AM
Converting a Primative byte array to a Byte array object Kirby Java 3 10-08-2004 03:01 AM
JNI byte array to string static Java 13 06-18-2004 04:50 PM
Appending byte[] to another byte[] array Bharat Bhushan Java 15 08-05-2003 07:52 PM



Advertisments