19

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?

2 Answers 2

20

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);
5
  • 2
    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?
    – namuol
    Commented Apr 3, 2013 at 6:38
  • Looks promising, but env->ExceptionOccurred() is true after calling GetMethodID for me
    – JonnyBoy
    Commented Mar 20, 2014 at 19:53
  • 1
    I think that this may be closer to the sought answer: stackoverflow.com/questions/12719766/…
    – Wheezil
    Commented Oct 13, 2014 at 22:55
  • 4
    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(). Commented Nov 7, 2015 at 0:52
  • Thank you @MartinC.Martin. I need to first use the getClass() then use the getName(), as you described. Before it doesn't works. :\
    – Inkeliz
    Commented May 9, 2020 at 10:39
4

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;
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.