• Post Reply Bookmark Topic Watch Topic
  • New Topic

JNI - jstring / jobject/ char *

 
Rich Stepanski
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've written several functions in C++ that need to be passed char *. These need to be passed to the function from java....


So, my question is - what variable type should I pass in from java AND how can I easily convert that to char *s in C++ (I know this is a java forum)?

I've tried passing char []s as jcharArray objects. I couldn't implement this correctly and my program got garbage values.

I tried to use..

(this is C++)
[CODE]
JNIEXPORT jint JNICALL Java_..._some method(JNIEnv *environment, jobject instance, jch arArray name, jcharArray pass){
jboolean isCopy;

char * myCharPtr = environment->GetCharArrayElements(name, &isCopy);

... (char * passed on - contains garbage values)...
[/CODE


I also tried to pass Strings from my java program. The JNI header took these as jobjects. I didn't know a easy / correct way to convert these to jstrings or char []. Thanks for any help.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rich,

With JNI, as with many things, there's a right way and a wrong way to do everything, and unfortunately the right way isn't obvious. But you're in luck, as the right way is documented in detail here. In particular, this page tells you all you need to know about String parameters.

In short, you pass String from Java, and use the function GetStringUTFChars to get a const char * out of the "jstring" parameter to your native function. When you're through with the const char *, don't forget to use ReleaseStringUTFChars to tell Java you're done with it.

And you use the tool "javah" to create your native function declarations, rather than creating them by yourself -- this way you always get the argument types right!
 
Rich Stepanski
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the quick reply. I need to read through those pages a few more times. But I have another question about my header file.

I used a form of javah built into my IDE to create my native header file. That turned my String parameters passed into jobjects.

Should I just cast this (the jobject parameter) to a jstring or do I have a problem with my generated header?

Note - this has worked fine creating previous native header files, I just never used Strings.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be completely correct, you would have to use jstring. But the reality is that jobject and jstring and jwhatever are essentially just typedefs for void* on pretty much all platforms, so you could get away with using jobject and just static-casting to jstring for the compiler's sake.

This is from jni.h on Linux:



So you see that these types are just pointers to dummy structs. But to be really pedantic about it, that javah-workalike is broken. For curiosity's sake, what IDE is this we're talking about? Are you using the newest version?
 
Rich Stepanski
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using Sun's NetBeans IDE 3.6. I downloaded a JNI extension for it to be able to generate the native headers.

It sounded like the JNI extension might of been a third party project when I downloaded it, so that could be the problem.

Thanks again for the help.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!