Juan Perez
Juan Perez

Reputation: 19

JNI GetObjectClass always returns java/lang/Class

env->CallVoidMethod is returning java/lang/Class when using

env->GetObjectClass(aobject); //aobject was the argument sent by JNI to C++

aobject is a Java object that implements an interface.

jobject obj = env->GetObjectClass(aobject) 

is supposed to return the Java object but instead is returning java/lang/Class

Upvotes: 1

Views: 2452

Answers (3)

Jacob Masen-Smith
Jacob Masen-Smith

Reputation: 11

I encountered this error and had to ask around at work before I got a real answer.

The problem is that when you designate your native method as static, it supplies an instance of the jclass, not a jobject instance of that class, as it's called from a static context. (If you call getCanonicalName() on that jclass, it will return your class's name.)

If the native method needs to be static, then you should pass in the instance as an argument if you need it. Otherwise, just make it not static and you should be all fixed.

Upvotes: 1

Juan Perez
Juan Perez

Reputation: 19

The answer to my problem was described in "The Java Native Interface - Programmer's Guide and Specification" by Shen Liang.

"You can use Call< Type >Method family of functions to invoke interface methods as well. You must derive the method ID from the interface type"

Upvotes: 0

user207421
user207421

Reputation: 310850

You haven't regenerated your .h/.c file since you removed 'static' so your JNI method signature doesn't match the Java one. You have an extra jclass in the argument list that is only there for static methods. So you're going to get some very strange execution.

Upvotes: 0

Related Questions