5

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 ?

1

1 Answer 1

4

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

1
  • I had to cache the jclass / jobj and the memthodID as well but it works. Still a ugly hack.
    – Intrepidd
    Jul 12, 2013 at 7:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.