Is there a way to get class name as a string from jclass ?

void Callback() {

if(env == NULL || gObj == NULL) return;

jclass cls = env->GetObjectClass(gObj);

if(cls){

jmethodID mid = env->GetMethodID(cls, "MyMethod", "()V");

if(mid)

env->CallVoidMethod(gObj, mid, NULL);

else

printf("mid is null\n");

}

}

In the above code, env & gObj are global variables. In some cases, I can get a valid value for 'mid', but in some cases GetMethodID returns null. So, I'm guessing that in such cases, GetObjectClass is returning a different class than expected(which is quite possible). Is there a way to get the class name as a string using jclass and jobject ?

[680 byte] By [kteegalaa] at [2007-11-27 7:34:17]
# 1
If you have the claas object, you should be able to simply call getName() - using JNI of course.
bschauwejavaa at 2007-7-12 19:14:45 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 2
Thanks. How do I do that in the above example? Didn't find any documentation on JNI getName() method
kteegalaa at 2007-7-12 19:14:45 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 3
> Thanks. How do I do that in the above example? Didn't> find any documentation on JNI getName() methodgetName is a java method, not jni. You look in the Java API docs for it. As a java method you use JNI to call it.
jschella at 2007-7-12 19:14:45 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 4
Just an additional bit of informtion. This is a method of Object, and every java Object is a subclass of Object, and inherits the method.
bschauwejavaa at 2007-7-12 19:14:45 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 5

I realized later that it's a java method and I tried to get toString() method.

if(env == NULL || gObj == NULL) return;

jclass cls = env->GetObjectClass(gObj);

if(cls){

jmethodID mid = env->GetMethodID(cls, "toString", "()Ljava/lang/String;");

if(mid)

jstring str = (jstring) env->CallObjectMethod(gObj, mid, NULL);

else

printf("mid is null\n");

}

The interesting part is when I tried calling toString() method , the methodID again returns null. So, looks like something is wrong with my global variable g_Obj itself. It is created from a java object in another jni method as follows:

g_obj = env->NewGlobalRef(jobj);

I'll look more into this..Thanks.

kteegalaa at 2007-7-12 19:14:45 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 6

> void Callback() {

>if(env == NULL || gObj == NULL) return;

>jclass cls = env->GetObjectClass(gObj);

> if(cls){

> jmethodID mid = env->GetMethodID(cls,

> "MyMethod", "()V");

>if(mid)

>env->CallVoidMethod(gObj, mid, NULL);

>else

>printf("mid is null\n");

>

>

> In the above code, env & gObj are global variables.

> In some cases, I can get a valid value for 'mid', but

> in some cases GetMethodID returns null. So, I'm

> guessing that in such cases, GetObjectClass is

> returning a different class than expected(which is

> quite possible).

If it returns an unexpected class, what do you intend to do with it even if you have the name? Can't you use the JNI

IsInstanceOf

method to separate the bad objects from the good?

Jim S.

Niceguy1a at 2007-7-12 19:14:45 > top of Java-index,Java HotSpot Virtual Machine,Specifications...