No, it is absolutely NOT safe to call such a static function using a null (or invalid) class pointer.
Your practice can be very successful, for example, if your static method does not apply to any other member of the static class. However, if your static java method refers to any other static member, your JVM will need a valid class pointer.
Example:
Take this simple Java MyTest.java :
public class MyTest { public static void mymain() { System.out.println("Hello, World in java from mymain"); System.out.println(magic_counter);
And call it with the following JNI C ++ code snippet
... // JVM already loaded and initialised jclass cls2 = env->FindClass("MyTest"); if(cls2 == nullptr) { cerr << "ERROR: class not found !"; } else { cout << "Class MyTest found" << endl; jmethodID mid = env->GetStaticMethodID(cls2, "mymain", "()V"); if(mid == nullptr) cerr << "ERROR: method void mymain() not found !" << endl; else { env->CallStaticVoidMethod(cls2, mid); cout << endl; } }
Call GetStaticMethodID(nullptr, "mymain", "()V"); will fail. Because when mymain() is executed, it will try to access the magic_number static variable. Then the JVM will use the class pointer you provided, and assume that the vaild pointer is returned by the loaded class. But since it is zero, the system will be segfault.
Christophe
source share