JNI中jclass的类名 [英] Class name from jclass in JNI

查看:603
本文介绍了JNI中jclass的类名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个愚蠢的问题,表明缺乏对JNI的理解.我正在写一个封装Java VM的C ++程序:我正在使用CallVoidMethod之类的调用在VM中调用函数.这纯粹是背景,与问题无关.

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.

我希望能够找到给定jclass实例的Java类的名称.有什么办法吗?我可以像在Java程序中那样调用GetName函数吗?

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?

推荐答案

jclass实例是将在其上调用方法的对象;您需要在Class类上查找getName方法ID,然后使用CallObjectMethodjclass实例上调用它以获得jstring结果.

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.

简而言之,是的,您只需调用getName函数并查看jstring结果.

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

编辑

(消除了错误处理)

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);

这篇关于JNI中jclass的类名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆