当前位置: 首页 > 知识库问答 >
问题:

JNI在应用程序中检测到错误:调用JNI GetMethodID时出现挂起的异常java.lang.ClassNotFoundException:未找到类

梁楷
2023-03-14

我得到以下错误:

JNI在应用程序中检测到错误:调用JNI GetMethodID时出现挂起的异常java.lang.ClassNotFoundException:在路径:DexPathList[[zip文件“/system/framework/sample.jar”],NativeLibraryDirectories=[/vendor/lib,/system/lib]]上找不到类“package.name.class”

JNI DETECTED ERROR IN APPLICATION: JNI GetMethodID called with pending exception java.lang.ClassNotFoundException: Didn't find class "package.name.class" on path: DexPathList[[zip file "/system/framework/XposedBridge.jar"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
26771-26918/package.name.class A/art: art/runtime/java_vm_ext.cc:410]   at java.lang.Class dalvik.system.BaseDexClassLoader.findClass(java.lang.String) (BaseDexClassLoader.java:56)
26771-26918/package.name.class A/art: art/runtime/java_vm_ext.cc:410]   at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String, boolean) (ClassLoader.java:511)
26771-26918/package.name.class A/art: art/runtime/java_vm_ext.cc:410]   at java.lang.Class java.lang.ClassLoader.loadClass(java.lang.String) (ClassLoader.java:469)

以下是关于该错误的更多信息:

`jmethodID MethodId;
    JNIEnv * env;
    int nStatus = gVM->GetEnv((void**)&env, JNI_VERSION_1_6);
    int nResult = attachStatus(nStatus, &env);
    LOG_HM("::attach status = %d::\n", nResult);
    if (nResult == ATTACH_FAILED) {
        LOG_HM("Attach failed");
        return ;
    }

jclass classClass = env->GetObjectClass(gTotalUsageClass);
    auto classLoaderClass = env->FindClass("java/lang/ClassLoader");
    auto getClassLoaderMethod = env->GetMethodID(classClass, "getClassLoader",
                                             "()Ljava/lang/ClassLoader;");
    gClassLoader = env->CallObjectMethod(gTotalUsageClass, getClassLoaderMethod);
    gFindClassMethod = env->GetMethodID(classLoaderClass, "findClass",
                                    "(Ljava/lang/String;)Ljava/lang/Class;");

    jclass totalUsage = static_cast<jclass>(env->CallObjectMethod(gClassLoader, gFindClassMethod, env->NewStringUTF("com/a/a/TotalUsageInfo")));

    TotalUsageInfo *info = NULL;
    jobject jUsageInfo = NULL;

        jlong noOfProc = 0;
        jlong memTotal = 0;
        jlong memFree = 0;
        jlong cache = 0;
        jlong buffer = 0;
        jlong cpuUtil = 0;
        //jclass totalUsage = env->FindClass("com/a/a/TotalUsageInfo");
        if (totalUsage == NULL) {
            LOG_HM("Class TotalUsageInfo not Found \n");
            nResult = FAIL;
            detachThread(nResult);
            return;
        }
        else {
             LOG_HM("Class TotalUsageInfo Found \n");
        }

        jmethodID constructor = env->GetMethodID(totalUsage, "<init>", "(JJJJJJ)V");
        if (NULL == constructor) {
            LOG_HM("JNIGetTotalUsageInfo::TotalUsageInfo constructor not found\n");
            nResult = FAIL;
            detachThread(nResult);
            return ;
        }`

共有1个答案

尹辰沛
2023-03-14

为了调用getMethodid,您需要将C++线程正确地连接到主Java线程。像这样:

bool attachThreadToJVM(JNIEnv **env)
{

    bool ret = false;

    if(globalJavaVm != NULL) {
        JavaVMAttachArgs vmAttachArgs;
        vmAttachArgs.version = JNI_VERSION_1_6;
        vmAttachArgs.name = NULL;
        vmAttachArgs.group = NULL;
        jint attachRet = globalJavaVm->AttachCurrentThread(env, &vmAttachArgs);

        if(attachRet == 0)
        {
            ret = true;
        }
    }

    return ret;
}
 类似资料: