Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JNI DETECTED ERROR IN APPLICATION: JNI GetMethodID called with pending exception java.lang.ClassNotFoundException: Didn't find class

I am getting the following error:

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/sample.jar"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]

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)

Here is more about the error:

`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 ;
        }`
like image 986
andy Avatar asked Oct 30 '22 22:10

andy


1 Answers

In order to call GetMethodID, you need to properly attach your C++ thread to the main Java thread. Like this:

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;
}
like image 74
IgorGanapolsky Avatar answered Nov 15 '22 06:11

IgorGanapolsky