Forum Stats

  • 3,826,778 Users
  • 2,260,707 Discussions
  • 7,897,072 Comments

Discussions

Any way to get fully qualified class name of Java class from C/C++?

fbsd30
fbsd30 Member Posts: 2
edited Jul 10, 2014 5:31PM in Java Native Interface (JNI)

Hello all,

I am trying out some test JNI code before starting a project that will use a lot of Java API from C++.  I have seen old questions on the same topic and the general responses have always been you can call the getName method on a jclass instance.  However, with my test code always fails to find the getName method (I have other code that works with accessing static methods, inherited methods, non-virtual invocation of super-class methods etc).  Here is my code where I have tried various ways to get at the class name

        void classMethod()
        {
          if ( ! child ) return;

          jmethodID mid = env->GetMethodID( child, "getClass", "()Ljava/lang/Class;" );

          if ( mid )
          {
            jclass clsObj = static_cast<jclass>( env->CallObjectMethod( obj, mid ) );
            if ( ! clsObj ) std::cerr << "classMethod - Cannot retrieve Class object" << std::endl;

            mid = env->GetMethodID( clsObj, "getName", "()Ljava/lang/String;" );
            if ( mid )
            {
              jstring str = static_cast<jstring>( env->CallObjectMethod( clsObj, mid ) );
              displayString( "classMethod", str );
            }
            else
            {
              std::cout << "classMethod - Cannot find getClass.getName, trying direct getName" << std::endl;

              mid = env->GetMethodID( child, "getName", "()Ljava/lang/String;" );

              if ( mid )
              {
                jstring str = static_cast<jstring>( env->CallObjectMethod( clsObj, mid ) );
                displayString( "classMethod", str );
              }
              else std::cerr << "classMethod - Cannot find getClass.getName or getName directly on jclass" << std::endl;
            }
          }
          else std::cerr << "classMethod - Cannot find getClass" << std::endl;
        }

Note that if I just find "java/lang/Class" and then look up getName on it, it works fine.

Environment:

Mac OS X Mavericks

java version "1.7.0_51"

Java(TM) SE Runtime Environment (build 1.7.0_51-b13)

Java HotSpot(TM) 64-Bit Server VM (build 24.51-b03, mixed mode)

Thanks

Rakesh

Answers

  • fbsd30
    fbsd30 Member Posts: 2

    Just to clarify, the main intention is not just to get the class name, but eventually I will be a lot more interested in the other methods in the Class such as isInterface or other similar methods I can use to introspect the class.  I forgot to post the response I see when running my test code:

    classMethod - Cannot find getClass.getName, trying direct getName
    classMethod - Cannot find getClass.getName or getName directly on jclass
    
  • jschellSomeoneStoleMyAlias
    jschellSomeoneStoleMyAlias Member Posts: 24,877 Gold Badge

    If the methods do not return a value then an exception was thrown.  You MUST look for that exception.  You can't proceed after the exception.  There are some methods used to manage exceptions.

This discussion has been closed.