JNI- FindClass 函数返回 null

2022-09-04 05:34:52

我有一个简单的java类(“MainX”),我使用shell脚本和eclipse进行编译。当我调用 env->FindClass(“MainX”) 函数时,从脚本生成的 MainX.class 文件返回 null,而从 eclipse 生成的 MainX.class 文件返回类,然后执行 runMainX 函数。

生成的 MainX.class 文件与 JNI C++可执行文件位于同一文件夹中。

MainX.java

public class MainX {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println(new MainX().runMainX());
    }

    public String runMainX(){
        return ("0.789");
    }
}

JNIBinding.cpp

#define USER_CLASSPATH "."
....
....

JNIEnv* createVM (JavaVM **jvm)
{
    JNIEnv *env;                     /* pointer to native method interface */
    JavaVMInitArgs vm_args;              /* JDK/JRE 6 VM initialization arguments */
    JavaVMOption* options = new JavaVMOption[1]; //holds various JVM optional settings

    options[0].optionString = const_cast<char*>("-Djava.class.path="USER_CLASSPATH);
    vm_args.version = JNI_VERSION_1_6;       //version of Java platform
    vm_args.nOptions = 1;
    vm_args.options = options;
    vm_args.ignoreUnrecognized = false;
    /* load and initialize a Java VM, return a JNI interface * pointer in env */
    long status = JNI_CreateJavaVM(jvm, (void**)&env, &vm_args);

    if (status == JNI_ERR){
       cout << "Fail: Unable to load JVM \t Exit" << endl;
    }
    else if (status == JNI_OK){
    cout << "CreateVM:\t\tJVM loaded successfully!" << endl ;
    }

    delete options;
    return env;
}

....
....

float invokeMainX(JavaVM **jvm, JNIEnv *env){

    jclass    mainClass ; //Returns a class object from a fully-qualified name, or NULL if the class cannot be found.
    jmethodID classConstructor; //Returns the method ID for an instance (nonstatic) method of a class 
    jobject   classObject;  //Constructs a new java object
    jmethodID methodid;

    float outcome = 0;

    mainClass = env->FindClass("MainX");    //Returns a class object from a fully-qualified name, or NULL if the class cannot be found.
    if (mainClass==0) return 0;
         classConstructor = env->GetMethodID(mainClass, "<init>", "()V"); //Returns the method ID for an instance (nonstatic) method of a class 
    if (classConstructor==0) return -1;  
         classObject = env->NewObject(mainClass, classConstructor); //Constructs a new java object
    if (classObject==0) return -2;  
         methodid = env->GetMethodID(mainClass, "runMainX", "()Ljava/lang/String;");
    if (methodid==0) return -3;
            jstring result = (jstring)env->CallObjectMethod(classObject, methodid); //returns the result of the calling method, an object 

....
....
}

有人可以解释一下为什么会发生这种情况吗?

我感谢任何帮助。

任何想法???提前致谢


答案 1

JNI 文档中获取:FindClass

name: a fully-qualified class name (that is, a package name, delimited by "/", 
      followed by the class name).

所以假设类在包中,我想你必须替换your.package.name

mainClass = env->FindClass("MainX");

mainClass = env->FindClass("your/package/name/MainX");

希望这有帮助!


答案 2

我不确定您的平台上是否有这个问题,但我在Android平台上遇到了类似的问题。

FindClass 方法应仅从 Java 线程调用。FindClass 的实现是通过遍历当前的调用堆栈来寻找 ClassLoader。由于您尝试从本机线程调用 FindClass,因此没有要查找的 ClassLoader。看看这个JNI常见问题解答

如果类名看起来正确,则可能遇到类装入器问题。FindClass 希望在与代码关联的类装入器中启动类搜索。它检查调用堆栈,其外观如下所示:

Foo.myfunc(Native Method)
Foo.main(Foo.java:10)
dalvik.system.NativeStart.main(Native Method)

最上面的方法是Foo.myfunc。FindClass 查找与 Foo 类关联的 ClassLoader 对象并使用该对象。


推荐