簡體   English   中英

通過JNI / C ++調用時找不到Java方法

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

我的目標是構建一個Java-Class,它實現了一個從C ++調用的方法。 此方法在同一個類中獲取另一個Java方法的名稱。 通過java-reflection API,我想獲得它的引用(稍后再調用它)。

但是從C ++調用的方法沒有找到其他Java方法。 如果它從java運行它可以正常工作。 我錯過了什么?

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);
}

我不確定你是否可以將jni中的字符串與equality ==運算符進行比較。 代替

if (method == m.getName())

你應該試試

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

那里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM