问题
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