gpt4 book ai didi

java - matlab 调用 Findclass JNI 崩溃

转载 作者:太空宇宙 更新时间:2023-11-04 14:05:05 26 4
gpt4 key购买 nike

我正在尝试使用 JNI 从 simulink Matlab 调用 java 方法。我用 C++ 开发了一个小代码,它调用一个主要方法,该方法只在屏幕上打印 helloworld 作为第一次测试,但在它调用以查找类的那一行,matlab 崩溃了。C++ 代码是这样的:

#include <stdio.h>
#include <jni.h>
#include "mex.h"
class MatlabAmbassador {

public:
MatlabAmbassador ();
//Destructor
~MatlabAmbassador ();
void run();
JNIEnv* create_vm() ;
void invoke_class(JNIEnv* env);
private:

}; // end class

MatlabAmbassador::MatlabAmbassador() {

}

MatlabAmbassador::~MatlabAmbassador() {

}


// -------------------------------------------------------------------------

JNIEnv* MatlabAmbassador::create_vm() {
JavaVM* jvm;
JNIEnv* env;
JavaVMInitArgs args;
JavaVMOption options[1];
long status;
/* There is a new JNI_VERSION_1_4, but it doesn't add anything for the purposes of our example. */
args.version = JNI_VERSION_1_6;
args.nOptions = 1;
options[0].optionString = "-Djava.class.path=C:\\Apps\\Projetos em java\\Portico\\Portico_Agent\\bin";
args.options = options;
args.ignoreUnrecognized = JNI_FALSE;

status=JNI_CreateJavaVM(&jvm, (void **)&env, &args);
return env;
}
// -------------------------------------------------------------------------

void MatlabAmbassador::invoke_class(JNIEnv* env) {
jclass helloWorldClass;
jmethodID mainMethod;
jobjectArray applicationArgs;
jstring applicationArg0;

mexPrintf("First\n");
helloWorldClass = env->FindClass("Teste"); <--- MATLAB CRASHES HERE
mexPrintf("second\n");
if (env->ExceptionOccurred()) {
env->ExceptionDescribe();
env->ExceptionClear() ;
}
if ( helloWorldClass == NULL ) {
mexPrintf( "%s%s\n", "Unable to obtain class reference for ",
helloWorldClass );
return;
} else {
mexPrintf( "%s%s\n", "Sucessfully created class reference for ",
helloWorldClass );
}

mainMethod = env->GetStaticMethodID( helloWorldClass, "main", "([Ljava/lang/String;)V");

applicationArgs = env->NewObjectArray(1, env->FindClass("java/lang/String"), NULL);
applicationArg0 = env->NewStringUTF( "From-C-program");
env->SetObjectArrayElement( applicationArgs, 0, applicationArg0);

env->CallStaticVoidMethod( helloWorldClass, mainMethod, applicationArgs);
}

// -------------------------------------------------------------------------
void MatlabAmbassador::run() {

char str [80];

mexPrintf(" INITIALIZING....\n" );
JNIEnv* env = create_vm();
invoke_class( env );


}
// -------------------------------------------------------------------------

Simulink Matlab 有一些方法可以使用。此方法之一用于调用上述方法 run()。下面是一段代码:

static void mdlStart(SimStruct *S)
{
char *buf;
size_t buflen;
int status;

buflen = mxGetN((ssGetSFcnParam(S, 2)))*sizeof(mxChar)+1 ; // le o 3o parametro passado pela funcao (nome do arq)
buf = (char *)mxMalloc(buflen); // aloca memoria
status = mxGetString((ssGetSFcnParam(S, 2)), buf,(mwSize)buflen);

ssGetPWork(S)[0] = (void *) new MatlabAmbassador; // store new C++ object in the

MatlabAmbassador *c = (MatlabAmbassador *) ssGetPWork(S)[0];

c->run();

简单的java代码是:

public class Teste 
{
public static void main(String[] args)
{

System.out.println(" INITALIZING...");


}
}

那么,任何人都可以解释一下我遗漏了什么,或者解释一下在 Matlab 中调用 JNI 是否存在真正的问题。 Matlab版本为2011b,安装的java版本为JDK 1.0.6_45。

如有任何帮助,我将不胜感激。

最好的问候安德烈·努德尔andre.nudel@gmail.com

最佳答案

您确定 JNI_CreateJavaVM 成功了吗?您的代码未检查返回的状态代码或生成的 env 值(未初始化,因此它可能包含垃圾),并且崩溃显然发生在第一次尝试使用 env

如果您在 Matlab 中运行此程序,则 JVM 创建可能会失败,因为作为 Matlab 正常环境的一部分,进程内部已经运行了一个 JVM。 JNI 文档说“不支持在单个进程中创建多个 VM”(http://docs.oracle.com/javase/6/docs/technotes/guides/jni/spec/invocation.html#wp636)。如果发生这种情况,JNI_CreateJavaVM 将返回 JNI_EEXIST (-5)。即使这不是这里发生的事情,检查从可能失败的函数返回的状态代码也是一种很好的做法。 JNI 文档中的示例“为清楚起见”省略了错误检查,但您应该将其包含在您实际要运行的代码中。

检查 JNI_CreateJavaVM 返回的状态并打印出来以确保它成功。并且可能将 env 初始化为 0,这样就可以清楚地知道您是从 JNI 获得指针还是只是随机数据。

关于java - matlab 调用 Findclass JNI 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17390463/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com