Class name from jclass in JNI

16,356

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

Solution 2

Inspired by the accepted answer, I put a function fitting my purposes

/**
 * JNI/C++: Get Class Name
 * @param env [in] JNI context
 * @param myCls [in] Class object, which the name is asked of
 * @param fullpath [in] true for full class path, else name without package context
 * @return Name of class myCls, encoding UTF-8
 */
std::string getClassName(JNIEnv* env, jclass myCls, bool fullpath)
{
    jclass ccls = env->FindClass("java/lang/Class");
    jmethodID mid_getName = env->GetMethodID(ccls, "getName", "()Ljava/lang/String;");
    jstring strObj = (jstring)env->CallObjectMethod(myCls, mid_getName);
    const char* localName = env->GetStringUTFChars(strObj, 0);
    std::string res = localName;
    env->ReleaseStringUTFChars(strObj, localName);
    if (!fullpath)
    {
        std::size_t pos = res.find_last_of('.');
        if (pos!=std::string::npos)
        {
            res = res.substr(pos+1);
        }
    }
    return res;
}
Share:
16,356

Related videos on Youtube

Nicole
Author by

Nicole

I'm a very private person and don't publicise any details.

Updated on June 04, 2022

Comments

  • Nicole
    Nicole almost 2 years

    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?

  • namuol
    namuol about 11 years
    I'm sorry, but my brain just started eating itself when it read your answer due to the nested-meta nature of this question. Could you maybe show a simple code example?
  • Wheezil
    Wheezil over 9 years
    I think that this may be closer to the sought answer: stackoverflow.com/questions/12719766/…
  • Martin C. Martin
    Martin C. Martin over 8 years
    I think the code is not quite right. If you have a jclass object, myJClass, then you still need to get the jclass object for java.lang.Class, and look up the getName() method on that. So you need two jclass objects: the one you started with (myJClass in my example above) and one representing java.lang.class. You pass the latter to GetMethodID() and the former to CallObjectMethod().