java—在调试构建中使用jni函数运行c++应用程序会导致分段错误

0aydgbwb  于 2021-07-13  发布在  Java
关注(0)|答案(0)|浏览(168)

我有一个c++应用程序,它使用jni访问java函数。只要我在release中构建我的应用程序,一切都是好的。但是当我在debug中构建应用程序时,应用程序一调用jni代码就会崩溃。
我在ubuntu14.04x64下使用qtcreator,gcc4.8.2和java1.7.0\u75
示例方法:

void JNICommunicator::queryJNI_deleteIndividual(std::string path, std::string individualName){
    if(jvmStatus != JNI_ERR){

        jclass cls = env->FindClass("de/myclasspacked/MyClass"); //when getting to this point, the application crashes

        if(env->ExceptionOccurred()){
            env->ExceptionDescribe();

        }

        if(cls != 0){
            jmethodID constructor = env->GetMethodID(cls, "<init>", "()V");
            jobject object = env->NewObject(cls, constructor);

            jmethodID mAC = env->GetMethodID(cls, "deleteIndividual", "(Ljava/lang/String;Ljava/lang/String;)V");

            if(mAC != 0){

                jstring path = env->NewStringUTF(owlPath.c_str());
                jstring individualNameTemp = env->NewStringUTF(individualName.c_str());
                jobject jAdd = env->CallObjectMethod(object, mAC, path, individualNameTemp);

                if(env->ExceptionOccurred()){
                    env->ExceptionDescribe();
                }
            }
        }
    }
}

你知道它为什么会崩溃,或者我如何让应用程序在调试下工作吗?如果没有使用调试函数的能力,很难开发应用程序。
谢谢你的问候
我从这里得到我的环境。这是我如何启动jvm的:

void initJVM(){
std::string dPath = "-Djava.class.path=/svn/owl/OWLApiExample/bin";
char *cstr = new char[dPath .length()+1];
strcpy(cstr, dPath .c_str());

options[0].optionString = cstr;
options[1].optionString = "-Djava.library.path=/svn/owl/OWLApiExample/lib";
options[2].optionString = "-verbose:jni";

vm_args.version = JNI_VERSION_1_6;
vm_args.nOptions = 1;
vm_args.options = options;
vm_args.ignoreUnrecognized = 0;

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

if(jvmStatus != JNI_ERR){
    //log
}else{

    jsize nVMs;
    jvmStatus = JNI_GetCreatedJavaVMs(NULL, 0, &nVMs);      // 1. just get the required array length
    jvmStatus = JNI_GetCreatedJavaVMs(&jvm, nVMs, &nVMs);   // 2. get the data

    jvmStatus = jvm->GetEnv((void**)&env, JNI_VERSION_1_6);

    if (jvmStatus == JNI_EDETACHED){

        if (jvm->AttachCurrentThread((void**) &env, NULL) != 0) {
            //log
        }else{

    }else if (jvmStatus == JNI_EVERSION) {
        //log
    }
}
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题