我使用JNI对Java类进行C调用。 其中一个Java类加载DLL,使用SWIG生成的代码调用该DLL。
问题是:当我在C中调用调用DLL的Java类时,我什么也得不到,它不起作用。
不幸的是,我无法看到背后的错误。
我该如何解决这个问题? 谢谢!
以下是我使用的代码:
/*
* java method that loads the DLL and that is called from C
* in the function "createInstance"(see below)
*/
public void loadIsystem(){
System.load("c:\\_MksProj\\TestHarness\\lib\\IConnectJNI.dll");
//System.loadLibrary(IConnectJNI);
ConnectionMgr connection = new ConnectionMgr();
}
/*
* the ConnectionMgr constructor
* makes a call to the SWIG created wrapper "connectJNI"
* and constructs the object. The most recently DLL is used.
* here the call gets broken
*/
public ConnectionMgr() {
this(connectJNI.new_ConnectionMgr__SWIG_0(), true);
//System.out.println("connectJNI.new_ConnectionMgr__SWIG_0()");
}
/*C code that calls the Java method(the env var is already instantiated)
*the call gets broken when the "connection" instance is created:
*ConnectionMgr connection = new ConnectionMgr();
*in the loadSystem method
*/
jobject createInstance(JNIEnv* env, char * className, char * contsructorSignature, ...)
{
jclass myclz = (*env)->FindClass(env, className);
jmethodID cons = (*env)->GetMethodID(env, myclz, "<init>", contsructorSignature);
jobject scObject = (*env)->NewObject(env, myclz, cons);
printf("class: %d\n", myclz);
printf("constructor: %d\n", cons);
printf("instance: %d\n", scObject);
jmethodID scMethod1 = (*env)->GetMethodID(env, myclz, "loadIsystem", "()V");
(*env)->CallVoidMethod(env, scObject, scMethod1);
return scObject;
}
可能与此问题有关: call Java from Matlab