0

I'm trying to call a static method myJavaStaticMethod in a class com.myapp.MyClass but I keep getting a ClassNotFoundException

The class is contained in a different module from the one the calling method callNativeMethod is contained :

Where the class is contained :

package com.myapp.MyClass;

Where the class is being called from -> The JNI :

package com.mynativemodule;

JNI DETECTED ERROR IN APPLICATION: JNI NewStringUTF called with pending exception java.lang.ClassNotFoundException: Didn't find class "com.myapp.MyClass" on path: DexPathList[[directory "."],nativeLibraryDirectories=[/system/lib64, /system_ext/lib64, /system/lib64, /system_ext/lib64]]

This is how I go about it :

JavaVM* g_vm;

void callNativeMethod(std::string revEventName, std::string revData) {
    JNIEnv * g_env;
    // double check it's all ok
    int getEnvStat = g_vm->GetEnv((void **)&g_env, JNI_VERSION_1_6);
    if (getEnvStat == JNI_EDETACHED) {
        std::cout << "GetEnv: not attached" << std::endl;
        if (g_vm->AttachCurrentThread(reinterpret_cast<JNIEnv **>((void **) &g_env), NULL) != 0) {
            __android_log_print(ANDROID_LOG_WARN, "MyApp", "Failed to attach");
        }
    } else if (getEnvStat == JNI_OK) {
        //
    } else if (getEnvStat == JNI_EVERSION) {
        __android_log_print(ANDROID_LOG_WARN, "MyApp","GetEnv: version not supported");
    }

    jclass MyClass = g_env->FindClass("com/myapp/MyClass");

    jmethodID revInitNativeEvent = g_env->GetStaticMethodID(MyClass, "myJavaStaticMethod", "()V");

    g_env->CallStaticVoidMethod(MyClass, revInitNativeEvent);

    if (g_env->ExceptionCheck()) {
        g_env->ExceptionDescribe();
    }

    g_vm->DetachCurrentThread();
}  

The JNIEnv setter :

Java_com_myapp_setJNIEnv(JNIEnv *env, jobject thiz) {
    env->GetJavaVM(&g_vm);
}

What am I doing wrong?

My guess is that it fails since the class is contained in a different module.

Any help will be much appreciated.

Thank you all in advance.

Program-Me-Rev
  • 6,184
  • 18
  • 58
  • 142
  • 1
    the class loader in non-java threads is very limited, you need to lookup classes on java threads or pass the class loader into your native threads – Alan Birtles Dec 01 '22 at 08:32

0 Answers0