pashkoff
pashkoff

Reputation: 147

FindClass and GetMethodID failes on android jni

I have c++ lib used with my application. I passed java object to jni and saved it to global reference. Then, I wish to call method of this java object from jni from antoher thread (I use pthread).

Java class is:

public class WaitingServiceReadyCallback {
   public void ready(String serviceName) throws Exception { ... // some code }
}

To call java method I use next code:

jvm->AttachCurrentThread(&env, 0);
cls = env->GetObjectClass(__obj__); // __obj__ is global reference to object.
if (!cls)
   goto detach;
mid = env->GetMethodID(cls, "ready", "(Ljava/lang/String;)V");

There GetMethodID fails to find method. When I use

cls = env->FindClass("com/mypackage/WaitingServiceReadyCallback");

instead of GetObjectClass, FindClass fails to find this class.

I tried to check class name of the object referencd by my __obj__ global reference (used getName from com/java/Class, made call to getName in the same place of my code as above call to ready), I got right class name - com.mypackage.WaitingServiceReadyCallback.

I am sure that class exists and loaded (java code executed before jni and instance of this class is created there), I am sure that method exists in the class.

So, I can't understand, what I done wrong?

Upvotes: 1

Views: 2825

Answers (1)

trashkalmar
trashkalmar

Reputation: 883

I met this problem. The reason in short: within another thread VM does not provide us an info about loaded classes. I've solved this by storing the classloader of some sample java object and then using it for manual loading of needed classes from another threads.

Upvotes: 2

Related Questions