Question

I am facing issues with making a java call from C++ code using JNI. I am able to get the jobject, but the invoocation of any API on the jobject fails. On digging for nearly one day and comparing with other working Java API (jobjects which i call in my code), i found one difference.

the following piece of code

void printClassInfo(JNIEnv* env, jobject object, jclass klazz)
{
    printf("printclass info 1\n");
    printf("printclass info 2\n");

    // First get the class object
    jmethodID mid = env->GetMethodID(klazz, "getClass", "()Ljava/lang/Class;");
    printf("printclass info 2.1\n");
    jobject clsObj = env->CallObjectMethod(object, mid);
    printf("printclass info 3\n");
    if(clsObj == NULL){
        printf("cls obj is null");
    }
}

prints cls obj is null for the jobject for which I am seeing issues.

For other jobjects, the call does not return null.

The major difference is that it is a newly added class and I seemed to have missed something that can cause this issue. I have rechecked again and again but not getting any clear indicators.

Any help appreciated.

Was it helpful?

Solution 2

I was able to find the solution. Heres what was wrong, in case it can be of help to others

the Java API was returning the List and in the JNI after the invocation, I was trying to treat it as a jobjectArray which obviously will not work because in JNI terms it will be a jobject and we have to treat as a jobject (correct me if this is wrong). I followed implementations that were already in place but seems they were never tested.

The behaviour that really puzzled me was that it never complained about the typecasting I was doing to convert it to a jobjectarray and even let me traverse the corrupted jobjectarray and even extract a corrupted element. That took me a while to troubleshoot.

all this made me think, languages like Haskell with their strong type inference ought to be more used for application softwares.

OTHER TIPS

You have the object already. Why do you need its class (sorry klass) at all? JNI has a nic function for you, GetObjectClass(jobject). Here is what you could do:

void printClassInfo(JNIEnv* env, jobject object) {

   jclass clsObj = env->GetObjectClass(env, object);
   if (clsObj == NULL) {
    printf("cls obj is null");
   }
}

Will this solve your problem?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top