使用JNI(C ++)从本机线程中调用Java方法的麻烦

 2023-01-20    313  

问题描述

我有一个JNI问题,我希望有人可以帮助我.
我正在尝试调用来自本机线程的Java类的构造函数.
它一直在此特定类的FindClass(…)中不断失败.

这是代码:

使用JNI(C ++)从本机线程中调用Java方法的麻烦

LOGE("1");
    JNIEnv *env = NULL;

    LOGE("2");
    int res = -1;
    res = g_vm->AttachCurrentThread(&env, NULL);

    if(env == NULL)
    {
        LOGE("env is NULL, AttachCurrentThread failed");;
    }


    if(res >= 0)
        LOGE("AttachCurrentThread was successful");
    jclass clazz = NULL;
    jmethodID cid;

    jclass clazzESEngine;
    jmethodID callbackid;

    jobject jCoreOut;
    static jfieldID fid_ActionState = NULL;
    static jfieldID fid_nSpeed = NULL;
    static jfieldID fid_nType = NULL;
    static jfieldID fid_nInProcess = NULL;
    static jfieldID fid_nX = NULL;
    static jfieldID fid_nY = NULL;
    LOGE("3");

    static const char* const ECOClassName = "lus/android/sdk/LUSOutputJNI";
    //static const char* const ECOClassName = "android/widget/TextView";
    clazz = env->FindClass(ECOClassName);
    if (clazz == NULL) {
        LOGE("Can't find class LUSOutputJNI");

    }
    else
        LOGE("lus/android/sdk/LUSOutputJNI was found, YEY!!");

    LOGE("4");
    cid = env->GetMethodID(clazz,"<init>", "()V");
    LOGE("5");
    jCoreOut = env->NewObject(clazz, cid);

    LOGE("6");

这是logcat输出失败时的输出:

E/lusCore_JNI( 3040): 1
E/lusCore_JNI( 3040): 2
E/lusCore_JNI( 3040): AttachCurrentThread was successful
E/lusCore_JNI( 3040): 3
E/lusCore_JNI( 3040): Can't find class LUSOutputJNI
E/lusCore_JNI( 3040): 4
W/dalvikvm( 3040): JNI WARNING: JNI method called with exception raised

观察:

  • 我从AttachCurrentThread(0为0)获得结果,这意味着此附件成功 + ENV指针不再为空.
  • 我确定lusoutputjni的软件包名称声明(三重检查…)
  • 当我尝试以更受欢迎的类名称(例如Android/Widget/textView)运行FindClass(..)时,我会得到一个积极的匹配.它在那里.表示线程附件和ENV变量还可以. (我可以假设吗?)
  • 当我尝试从具有运行它的JNI线程的JNI方法中运行以下代码时,它会发现lusoutputjni类没有问题.

我在做什么错?

帮助您将不胜感激:)

感谢您的时间,

ita

推荐答案

在此处找到了答案\. (寻找常见问题解答:” FindClass没有找到我的班级”
jni tips )

)

i BASIADY保存了所需的JCLASS对象的全局参考.
但是,确实必须克服C/JNI和C ++/JNI之间的一些邪恶的JNI变化,以使代码编译.
这就是我让newglobalref编译和工作的方式.

jclass localRefCls = env->FindClass(strLUSClassName);
if (localRefCls == NULL) {
    LOGE("Can't find class %s",strLUSCoreClassName);
    return result;
}

//cache the EyeSightCore ref as global
 /* Create a global reference */
 clazzLUSCore = (_jclass*)env->NewGlobalRef(localRefCls);

 /* The local reference is no longer useful */
 env->DeleteLocalRef(localRefCls);

 /* Is the global reference created successfully? */
 if (clazzLUSCore == NULL) {
     LOGE("Error - clazzLUSCore is still null when it is suppose to be global");
     return result; /* out of memory exception thrown */
 }

我希望这对任何人有帮助.

其他推荐答案

要获取有关出错的更多信息(它将登录到STDERR,而不是Loge,我不确定如何更改),您可以添加一些异常打印 – 您可以更改此信息:

//static const char* const ECOClassName = "android/widget/TextView";
clazz = env->FindClass(ECOClassName);
if (clazz == NULL) {
    LOGE("Can't find class LUSOutputJNI");

}
else
    LOGE("lus/android/sdk/LUSOutputJNI was found, YEY!!");

:

//static const char* const ECOClassName = "android/widget/TextView";
    clazz = env->FindClass(ECOClassName);
    if (clazz == NULL) {
        LOGE("Can't find class LUSOutputJNI");
        jthrowable exc = env->ExceptionOccurred();
        if(exc)
        {
          env->ExceptionDescribe();
          env->ExceptionClear();
        }    
    }
    else
        LOGE("lus/android/sdk/LUSOutputJNI was found, YEY!!");

以上所述是小编给大家介绍的使用JNI(C ++)从本机线程中调用Java方法的麻烦,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对77isp云服务器技术网的支持!

原文链接:https://77isp.com/post/26052.html

=========================================

https://77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。