In C/C++ you need a jclass value in order to register native functions to a Java class with methods declared native.
Consider this Java class:
public class Native {
public native int f(int i);
}
To register a native C/C++ function for Native.f() we need to call this on C++ side:
JNIEnv* env = ...;
jclass nativeClass = ...;
JNINativeMethod nativeMethod = {
(char*) "f",
(char*) "(I)I",
(void*) Java_org_example_Native_f // The native C function
};
env->RegisterNatives(nativeClass, &nativeMethod, 1);
The problematic part is to get the jclass value if the class is not accessible from the default class loader. This not the case if the class Native resides in an OSGi bundle loaded inside an OSGi container (e.g. Equinox) running inside the JVM.
To get to the class, I use a Java function to return the Class<?> instance of my class:
public class Helper {
public static Class<?> getNativeClass() {
// Find bundle from system bundle context.
Bundle bundle = ... // details left out.
return bundle.loadClass("org.example.Native");
}
}
This function resides in a class outside the OSGi container. Calling this function from the C++ side using JNI gives us a jobject which is an instance of java.lang.Class<>.
// Load the `Helper` class.
jclass helperClass = env->FindClass("org.example.Helper");
// Get the method `Helper.getNativeClass()`.
jmethodId mid = env->GetStaticMethodID(helperClass,
"getNativeClass",
"()Ljava/lang/Class;");
// Call `Helper.getNativeClass()`.
jobject nativeClassInstance = env->CallStaticObjectMethod(helperClass, mid);
The problem is that we really need a ‘jclass’ value (not a jobject value). To get it, I instantiate an object of my Native class and get the jclass from that.
// This is the jclass for 'java.lang.Class<>'.
jclass classClass = env->GetObjectClass(nativeClassInstance);
// Now get the method id for function 'newInstance()'
jmethodId newInstanceMid = env->GetMethodID(helperClass,
"newInstance",
"()Ljava/lang/Object;");
// Instantiate our 'Native' class calling 'Class<Native>.newInstance()'.
jobject nativeInstance = env->CallObjectMethod(classClass,
nativeClassInstance,
newInstanceMid);
// Now, I can get the desired 'jclass' of my 'Native' class.
jclass nativeClass = env->GetObjectClass(nativeInstance);
But this only works because I can instantiate my Native class.
Any ideas how to get the desired jclass without having to instantiate an object of the Native class?
You should be able to use the
jobjectof typeClass<Native>that’s returned by your helper method as ajclassby simply casting it: