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 !
Gordon Beaton - 05 Oct 2006 07:35 GMT
> 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.
>
[quoted text clipped - 3 lines]
> 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

Signature
[ 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
abhi147@gmail.com - 05 Oct 2006 07:58 GMT
Hi Gordon ,
Thanks a lot for your time . I need just one more help
> Here are a few things I can see directly:
>
[quoted text clipped - 7 lines]
>
> - Details should be array of jbyte*.
How can I convert the Details of jbyte* type to char* ?
Gordon Beaton - 05 Oct 2006 08:56 GMT
> 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

Signature
[ 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
abhi147@gmail.com - 05 Oct 2006 10:02 GMT
It's working .. Thanks a lot :-)
> > How can I convert the Details of jbyte* type to char* ?
>
[quoted text clipped - 9 lines]
> [ 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