Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble calling on a Java method from a native thread using JNI (C++)

I have a JNI problem which I hope someone can help me out with.
I'm trying to call on a constructor of a Java class called LUSOutputJNI from a native thread.
It keeps failing on FindClass(...) of this specific class.

Here is the code:

 LOGE("1");
    JNIEnv *env = NULL;

    LOGE("2");
    int res = -1;
    res = g_vm->AttachCurrentThread(&env, NULL);

    if(env == NULL)
    {
        LOGE("env is NULL, AttachCurrentThread failed");;
    }


    if(res >= 0)
        LOGE("AttachCurrentThread was successful");
    jclass clazz = NULL;
    jmethodID cid;

    jclass clazzESEngine;
    jmethodID callbackid;

    jobject jCoreOut;
    static jfieldID fid_ActionState = NULL;
    static jfieldID fid_nSpeed = NULL;
    static jfieldID fid_nType = NULL;
    static jfieldID fid_nInProcess = NULL;
    static jfieldID fid_nX = NULL;
    static jfieldID fid_nY = NULL;
    LOGE("3");

    static const char* const ECOClassName = "lus/android/sdk/LUSOutputJNI";
    //static const char* const ECOClassName = "android/widget/TextView";
    clazz = env->FindClass(ECOClassName);
    if (clazz == NULL) {
        LOGE("Can't find class LUSOutputJNI");

    }
    else
        LOGE("lus/android/sdk/LUSOutputJNI was found, YEY!!");

    LOGE("4");
    cid = env->GetMethodID(clazz,"<init>", "()V");
    LOGE("5");
    jCoreOut = env->NewObject(clazz, cid);

    LOGE("6");    

Here is the logcat output from when it fails:

E/lusCore_JNI( 3040): 1
E/lusCore_JNI( 3040): 2
E/lusCore_JNI( 3040): AttachCurrentThread was successful
E/lusCore_JNI( 3040): 3
E/lusCore_JNI( 3040): Can't find class LUSOutputJNI
E/lusCore_JNI( 3040): 4
W/dalvikvm( 3040): JNI WARNING: JNI method called with exception raised  

Observations:

  • I get a result from AttachCurrentThread which is 0, which means that this attachment was successful + the env pointer is no longer NULL.
  • I'm sure about the package name declaration of LUSOutputJNI (triple checked it...)
  • When I try to run FindClass(..) with a more popular class name such as android/widget/TextView , I get a positive match. It is there. Meaning the thread attachment and the env variables are ok. (Can I assume that?)
  • When I try to run the following code from a JNI method which has a JNI thread running it, it finds the LUSOutputJNI class without a problem.

What am I doing wrong?

Help will be much appreciated :)

Thanks for your time,

Ita

like image 850
Ita Avatar asked Sep 12 '11 20:09

Ita


2 Answers

Found the answer \ work around in here. (Look for FAQ: "FindClass didn't find my class" in JNI tips)

I basicaly saved a global ref to the needed jclass objects.
Did however had to overcome some evil JNI changes between C/JNI and C++/JNI in order for the code to compile.
This is how I got the NewGlobalRef to compile and work.

jclass localRefCls = env->FindClass(strLUSClassName);
if (localRefCls == NULL) {
    LOGE("Can't find class %s",strLUSCoreClassName);
    return result;
}

//cache the EyeSightCore ref as global
 /* Create a global reference */
 clazzLUSCore = (_jclass*)env->NewGlobalRef(localRefCls);

 /* The local reference is no longer useful */
 env->DeleteLocalRef(localRefCls);

 /* Is the global reference created successfully? */
 if (clazzLUSCore == NULL) {
     LOGE("Error - clazzLUSCore is still null when it is suppose to be global");
     return result; /* out of memory exception thrown */
 }  

I hope this helps anyone.

like image 122
Ita Avatar answered Oct 05 '22 22:10

Ita


To get a bit more information about whats going wrong ( it will log to stderr, not LOGE, and I'm not sure how to change that) you can add some exception printing - you can change this:

//static const char* const ECOClassName = "android/widget/TextView";
clazz = env->FindClass(ECOClassName);
if (clazz == NULL) {
    LOGE("Can't find class LUSOutputJNI");

}
else
    LOGE("lus/android/sdk/LUSOutputJNI was found, YEY!!");

To this:

    //static const char* const ECOClassName = "android/widget/TextView";
    clazz = env->FindClass(ECOClassName);
    if (clazz == NULL) {
        LOGE("Can't find class LUSOutputJNI");
        jthrowable exc = env->ExceptionOccurred();
        if(exc)
        {
          env->ExceptionDescribe();
          env->ExceptionClear();
        }    
    }
    else
        LOGE("lus/android/sdk/LUSOutputJNI was found, YEY!!");
like image 44
Michael Anderson Avatar answered Oct 06 '22 00:10

Michael Anderson