JNI with multidimensional Byte Array

A

abhi147

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 !
 
G

Gordon Beaton

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
 
A

abhi147

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* ?
 
G

Gordon Beaton

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
 
A

abhi147

It's working .. Thanks a lot :)

Gordon said:
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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

JNI and GetFieldID 12
JNI Error in passing array 0
JNI- Problem with setObjectArrayElement 0
JNI 1
jni libjvm sigsegv 1
Problem with JNI 7
JNI error with JDK1.5 1
JNI problem with GetStringUTFLength 6

Members online

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,732
Latest member
ArronPalin

Latest Threads

Top