JNI: cannot find java class from native method in callback

I am new to JNI and I am struggling with this problem.

I have a C ++ callback called by the network library (alljoyn).

In this callback, I need to call the Java code.

Since this callback is in a different thread, I use the following code to get the JNIEnv pointer:

(jvm is a global pointer)

JNIEnv *env = NULL; jvm->AttachCurrentThread(&env, NULL); 

The problem is that when I try to call FindClass using this env pointer, the result will be NULL.

If I use the same FindClass call in my main thread, everything works fine.

How can i fix this? Is this somehow related to the classpath?

+4
source share
1 answer

The answer and the official workaround can be found on developer.android . If you need to go beyond caching global references for all classes that your own code might need, you will find a successful solution that caches the correct class loader here: FindClass from any stream in Android JNI

+3
source

Source: https://habr.com/ru/post/1490026/


All Articles