Discussion Locked

PassingvaluesfromnativecodetojavawithJNI

0 helpful
3 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I want to pass strings from my C Program to java
code using jni.How do I do that?

3 replies

by geert pante

If your java native method has a java.lang.String as input and another as output, javah compiles it to:

JNIEXPORT jstring JNICALL Java_package_ClassName_methodName
(JNIEnv* env, jclass cl, jstring input)

Jstring's are C structs that can be converted to char* using (*env)->GetStringUTFChars and back using (*env)->NewStringUTF.

{
const char* str_in;
char* str_out;
jstring ret;
str_in = (*env)->GetStringUTFChars(env, input, NULL);
/*do something with str_in and str_out*/
ret = (*env)->NewStringUTF(env, str_out);
return ret;
}

See:
http://java.sun.com/docs/books/tutorial/native1.1/implementing/string.html

Greetings, Geert.

This discussion is closed to new replies.