找不到JNI课程

时间:2017-06-30 09:14:56

标签: java c++ qt java-native-interface

我尝试通过C ++ / Qt程序实现JNI来访问JAVA类。

我按照这个例子进行了一个小部分。 https://www.codeproject.com/Articles/993067/Calling-Java-from-Cplusplus-with-JNI 只需改变它以适应Qt程序。

现在我尝试了几天来解决这个问题"没有找到课程"错误。 我在这个问题上阅读了很多其他问题,并在options [0] .optionString中使用不同的路径符号。 test.class文件和.exe一样在同一目录下,我检查过这是程序的CWD。

我是否忽略了一些显而易见的事情,还是会出现更多问题?

非常感谢!

#include "MyXPS.h"

JavaVM *jvm;                      // Pointer to the JVM (Java Virtual Machine)
JNIEnv *env;                      // Pointer to native interface


MyXPS::MyXPS(void)
{
}

MyXPS::~MyXPS(void)
{
    jvm->DestroyJavaVM();
}

QString MyXPS::InitializeJNI()
{
        //================== prepare loading of Java VM ============================
    JavaVMInitArgs vm_args;                        // Initialization arguments
    JavaVMOption* options = new JavaVMOption[1];   // JVM invocation options
    options[0].optionString = "-Djava.class.path=.";   // where to find java .class "-Djava.class.path=c:\\Users\\admin\\Desktop\\Release" "-Djava.class.path=C:/Users/admin/Desktop/Release/"  (char *)
    vm_args.version = JNI_VERSION_1_6;             // minimum Java version
    vm_args.nOptions = 1;                          // number of options
    vm_args.options = options;
    vm_args.ignoreUnrecognized = false;     // invalid options make the JVM init fail
        //=============== load and initialize Java VM and JNI interface =============
    jint rc = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);  // YES !!
    delete options;    // we then no longer need the initialisation options. 

    if (rc != JNI_OK) {
        // TO DO: error processing...
        if(rc == JNI_EVERSION)
            return "FATAL ERROR: JVM is oudated and doesn't meet requirements";
        else if(rc == JNI_ENOMEM)
            return "FATAL ERROR: not enough memory for JVM";
        else if(rc == JNI_EINVAL)
            return "FATAL ERROR: invalid ragument for launching JVM";
        else if(rc == JNI_EEXIST)
            return "FATAL ERROR: the process can only launch one JVM an not more";
        else
            return "FATAL ERROR:  could not create the JVM instance (error code " + QString::number(rc) + ")";
    }   
    return "Initialization successfull";
}

QString MyXPS::getVersion()
{
       //=============== Display JVM version =======================================
    jint ver = env->GetVersion();
    //return((ver>>16)&0x0f)+"."+(ver&0x0f);
    return QString::number(ver);
}

QString MyXPS::test(double height, double weight)
{
    jclass cls2 = env->FindClass("test");  // try to find the class
    if(cls2 == 0) 
    {
        return "class not found";
    }

    else 
    {                                  // if class found, continue
        //cout << "Class MyTest found" << endl;
        jmethodID mid = env->GetStaticMethodID(cls2, "demo", "(DD)Ljava/lang/String;");  // find method
        if(mid == NULL)
        {
            //cerr << "ERROR: method void mymain() not found !" << endl;
            return "ERROR: method void mymain() not found !";
        }
        else 
        {
            jobject result = env->CallStaticObjectMethod(cls2, mid, (jdouble)height, (jdouble)weight);                      // call method
            const char* str = env->GetStringUTFChars((jstring) result, NULL);
            QString Qresult = QString::fromLocal8Bit(str);
            return Qresult;
        }
    }
}

好的,我实现了一些代码来获取异常,它是NoClassDefFoundError。所以它不是关于.class文件的路径。现在我将检查Java中的编译和朗读路径。

1 个答案:

答案 0 :(得分:0)

我知道这不是你的确切解决方案,但你可以看一下:

http://jnicookbook.owsiak.org/recipe-no-027/

此示例显示如何从C代码调用类。看起来在您的情况下,问题在类路径中是相关的。使用 test 类确保您的C代码知道 .class 文件的确切位置