Findclass result is 0 when recalling Java method using JNI in c++.++

Asked 1 years ago, Updated 1 years ago, 88 views

 JavaVMOption options;
    JavaVMInitArgs vm_args;
    JNIEnv* env;
    JavaVM* jvm;
    long status;

    options.optionString = "-Djava.class.path=D:\\DEV\\javaworkspace\\Open";
    vm_args.version = JNI_VERSION_1_8;
    vm_args.nOptions = 1;
    vm_args.options = &options;

    status = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);

    jclass cls;
    jmethodID method;

    if (status != JNI_ERR)
    {
        Find cls = env->FindClass("Open"); // class to create a jclass.

        if (cls != 0)
        { 
            method = env->GetStaticMethodID(cls, "openFile", "(Ljava/lang/String;ILjava/lang/String;)V");
            if(method != 0)
            {
                jstring method_args0 = env->NewStringUTF("");  // path
                jint method_args1 = 0;  // pageIndex
                jstring method_args2 = env->NewStringUTF("");  // search term
                env->CallStaticVoidMethod(cls, method, method_args0, method_args1, method_args2);
            }

        }
        else
        {
            AfxMessageBox(_T ("Class not found")"));
            return;
        }

        jvm->DestroyJavaVM();
    }

I'm trying to load the openFile (String path, int index, String term) method in a class called Open, but I couldn't find the class when I did Findclass ("Open"). When tested with Findclass ("java/lang/String"), the results return well, so it seems that the path to the Open Class is also a problem.Please help me (The class file exists within D:\DEV\javaworkspace\Open\src\mv.)

c++ jni

2022-09-21 15:55

1 Answers

There is a high probability of a class pass problem.

https://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/invocation.html

Look at the and

Please set the classpath for JavaVMOption.

options[0].optionString = "-Djava.class.path=D:\DEV\javaworkspace\Open\src";

Please refer to .


2022-09-21 15:55

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.