简体   繁体   中英

Java method is not found when called through JNI/C++

My goal is to build a Java-Class witch implements a method called from C++. This method gets the name of another Java-Method in the same class. Through the java-reflection API I want to get a reference to it (and call it later).

But the method called from C++ doesn't find the other Java method. If it runs from java it works fine. What do I miss?

JAVA:

public void myCPlusPlusFunc(String method){ // I'll pass "noparam" in here
    logMessage("Searching for method " + method + "....");
    for (Method m : this.getClass().getMethods()) {
        if (method == m.getName()) {
            logMessage("Found it!"); // never found when called through JNI/C++
            // (...) invoke the method etc...
        }
    }
}

public void noparam() {
    logMessage("noparam got called");
}

C++

JNIEnv *env = theJVMLoader->getENV();
jmethodID m = env->GetMethodID(getBeanClass(), "myCPlusPlusFunc", "(Ljava/lang/String;)V");
if (env->ExceptionCheck()) {    
    handleException();
    ASSERT(FALSE);
    return FALSE;
}
ASSERT(m);
if (m)
{
    // "noparam" is the method i expect to find
    jstring s = env->NewStringUTF("noparam"); 
    env->CallVoidMethod(getBeanInstance(), m, s);
}

Im not sure if you can compare strings from jni with the equality == operator. Instead of

if (method == m.getName())

you should try

if (m.getName().equals(method))

there

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM