0

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.

3 Answers 3

2
  1. stop using wrong manual names for JNICALL functions. 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.
  2. then of course this correct function name must be used in the last member of JNINativeMethod struct
  3. there is no such class as java/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.
  4. ADDED (thanks @EJP): stop using wrong manual strings for JNI signatures and use the output of javap -s instead

Your 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.

Sign up to request clarification or add additional context in comments.

3 Comments

Good advice. I would add 'stop using wrong manual strings for JNI signatures and use the output of javap -s instead'. That would solve (3). +1
I prefer the 'stop using' wording that agrees with your own point 1.
@EJP i wanted to avoid sounding too strong. But for no apparent reason besides vague humanistic feeling, i admit.
1

const_cast("(Ljava/lang/jsting)Ljava/lang/jsting;") spell error, should be const_cast("(Ljava/lang/jsting)Ljava/lang/jstring;")

jsting ==> jstring

Comments

1
Copy some android source code to help you(JNI):


static jobject osNetworkSystem_getHostByNameImpl(JNIEnv* env, jclass clazz,
        jstring nameStr, jboolean preferIPv6Addresses) {

}

static void osNetworkSystem_setInetAddressImpl(JNIEnv* env, jobject obj,
        jobject sender, jbyteArray address) {

}

static jobject osNetworkSystem_inheritedChannelImpl(JNIEnv* env, jobject obj) {

}

/*
 * JNI registration.
 */
static JNINativeMethod gMethods[] = {
    /* name, signature, funcPtr */
    { "getHostByNameImpl",                 "(Ljava/lang/String;Z)Ljava/net/InetAddress;",                              (void*) osNetworkSystem_getHostByNameImpl                  },
    { "setInetAddressImpl",                "(Ljava/net/InetAddress;[B)V",                                              (void*) osNetworkSystem_setInetAddressImpl                 },
    { "inheritedChannelImpl",              "()Ljava/nio/channels/Channel;",                                            (void*) osNetworkSystem_inheritedChannelImpl               },
};

int register_org_apache_harmony_luni_platform_OSNetworkSystem(JNIEnv* env) {
    return jniRegisterNativeMethods(env,
            "org/apache/harmony/luni/platform/OSNetworkSystem",
            gMethods,
            NELEM(gMethods));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.