JNI : Unable to find java class from native method in a callback
android, java, java-native-interface
Solution
The answer and the official workaround can be found on developer.android. If you must go beyond pre-caching global references for all classes your native code might need, you will find a successful solution that caches the correct class loader here: FindClass from any thread in Android JNI
Problem
I'm fairly new with JNI and I'm struggling with this problem. I have a c++ callback being called by a network library (alljoyn). In this callback, I need to call Java code. Since this callback is in another thread, I use the following code to get a JNIEnv pointer : (jvm being a global pointer) ``` JNIEnv *env = NULL; jvm->AttachCurrentThread(&env, NULL); ``` Problem is, when I try to call FindClass with this env pointer, the result is NULL. If I do the exact same FindClass call within my main thread, everything works just fine. How can I fix this ? Is this somehow related to the classpath ?