• Post Reply Bookmark Topic Watch Topic
  • New Topic

JNI CallObjectMethod casting - Urgent  RSS feed

 
shankaran
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am new to JNI and have a question. I have a java class which returns back a bytes[]
I have the class jclass for the object and the jmethodid bytesMethodID which is mapped like this:
bytesMethodID = (*env)->GetMethodID(env, objcls, "mutableBytes", "()[B");

now in the c impementation I have a stuct pointer
DESIGN_STRUCT *str= NULL;
How do I assign the bytes[] into this struct pointer? If I do the following:

str = (*env)->CallObjectMethod(env, obj, bytesMethod());
I get a "assignment from incompatible pointer type" if I change that to:
str = (*env)->CallBytesMethod(env, obj, bytesMethod());]
I get a "assignment makes pointer from integer without a cast"
My question is what JNI function do i use to get back a byte[]
Any help and pointers greatly appreciated.
Thanks,
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The method will return not a char[], but a "jarray," an opaque token that refers to a Java array. To get the data out of the array (which you could then cast to a char *) you need to call special JNI array-access functions. There's a specific procedure you have to follow, and choices to make regarding copying the data or using direct access, etc. It's not rocket science but you do have to pay attention to the details.
The Java Tutorial covers JNI very well. Have a look at it here.
 
shankaran
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply but...
You are saying that the
str = (*env)->CallByteMethod(env, obj, bytesMethod());
will return a jarray instead of byte[]? The actual method in java returns a byte[]. I need to access the data directly. I did look at the documentation and also bought the book for JNI by Sheng Liang but stumped now... woudl appreciate any help.
 
shankaran
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just need some way to acess the byte[] directly, any code sample would do. Thanks a lot.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that's correct. When you call the Java method via JNI, in C you'll get a jarray, not a char*. To get the underlying data, you have to call more C functions.
OK, this is off the top of my head, so you need to double-check it, but basically, here's what you can do:
 
shankaran
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the code segment. I will try it out soon and let you know if the results. Thanks again!!
 
Jason Menard
Sheriff
Posts: 6450
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"java novice",
Welcome to JavaRanch. We don't have many rules here, but one of the ones we do have which we try to strictly enforce is our naming policy. Please change your display name to correspond with our guidelines.
Thank you for your cooperation and I look forward to your continued participation here at JavaRanch.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!