How to deliver a string array?
I post the code:
xx.cpp
JNIEXPORT jstring JNICALL Hello_Native(JNIEnv *env, jobject obj,jstring string)
{
const char *str = env->GetStringUTFChars(string, 0);
return env->NewStringUTF( "Hello from JNI !");
}
static JNINativeMethod gMethods[] = {
{"JniHello",const_cast<char*>("(Ljava/lang/jsting)Ljava/lang/jsting;"),(void*)Hello_Native}
xx.java
public native static String JniHello(String text);
System always prompt it has the problem when declare JniHello
in gMethods
and the parameter is not right.
javah
will generate it for you correctly. If your Java name is JniHello
in class MyHello
and your package is com.hello
, JNICALL function must be Java_com_hello_MyHello_JniHello
. It can't be Hello_Native
, you have made it up.JNINativeMethod
structjava/lang/jsting
. There is not even a java/lang/jstring
if i add the missing r
for you. You are asked for the JAVA signature, not JNI. So it must be java/lang/String
.javap -s
insteadYour code has one more problem: when used GetStringUTFChars
, you must also call `ReleaseStringUTFChars' before returning, otherwise you have a leak. But you will find this yourself sooner or later.