Discussions
Categories
- 196.9K All Categories
- 2.2K Data
- 239 Big Data Appliance
- 1.9K Data Science
- 450.3K Databases
- 221.7K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 550 MySQL Community Space
- 478 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3K ORDS, SODA & JSON in the Database
- 545 SQLcl
- 4K SQL Developer Data Modeler
- 187K SQL & PL/SQL
- 21.3K SQL Developer
- 295.9K Development
- 17 Developer Projects
- 138 Programming Languages
- 292.6K Development Tools
- 107 DevOps
- 3.1K QA/Testing
- 646K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 155 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.1K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 18 Java Essentials
- 160 Java 8 Questions
- 86K Java Programming
- 80 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 204 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 440 LiveLabs
- 38 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 171 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 232 Portuguese
Any way to get fully qualified class name of Java class from C/C++?

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
-
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
-
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.