Class name from jclass in JNI

元气小坏坏 提交于 2020-01-12 07:07:12

问题


This is probably a daft question that reveals a lack of understanding of JNI. I'm writing a C++ program that encapsulates the Java VM: I'm calling functions within the VM using calls such as CallVoidMethod. That's purely background and not very relevant to the question.

I would like to be able to find the name of the Java class given a jclass instance. Is there any way to do this? Could I just call the GetName function, as I would in a Java program?


回答1:


The jclass instance is your object on which a method will be invoked; you'll need to look up the getName method ID on the Class class, then invoke it on the jclass instance using CallObjectMethod to obtain a jstring result.

So in short yes, you just call the getName function and look at the jstring result.

EDIT

(error handling elided)

JNIEnv* env = ...;
// substitute your desired class's specifier for "java/lang/Class"...
jclass cls = env->FindClass("java/lang/Class"); 
jmethodID mid_getName = env->GetMethodID(cls, "getName", "()Ljava/lang/String;");
jstring name = env->CallObjectMethod(cls, mid_getName);


来源:https://stackoverflow.com/questions/9265457/class-name-from-jclass-in-jni

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!