背景
我在JAVA中有一个本地函数
package mypackage;
public class MyWrapper {
private native int
wrapFilterRawRequest(String config, String rawRequest);
public void
processRequest( String configPath,String rawRequest){
int status = 0;
status = new MyWrapper().wrapFilterRawRequest(configPath, rawRequest);
status2 = new MyWrapper().wrapFilterRawRequest(configPath, rawRequest);
}
static{
System.loadLibrary("mylibrary");
}
}
同时也需要一个在C语言中的JNI封装器
int processRequest(char *conf, char *req)
{
int status = 0;
// Do something here
return status;
}
JNIEXPORT jint JNICALL Java_mypackage_MyWrapper_wrapFilterRawRequest
(JNIEnv *env, jobject obj, jstring config, jstring rawRequest)
{
jint status = 0;
const char *conf, *req;
char *conf_copy, *req_copy;
conf = (env)->GetStringUTFChars(config, NULL);
req = (env)->GetStringUTFChars(rawRequest, NULL);
copy_str(&conf_copy, conf);
copy_str(&req_copy, req);
(env)->ReleaseStringUTFChars(config, conf);
(env)->ReleaseStringUTFChars(rawRequest, req);
status = processRequest(conf_copy , req_copy );
return status;
}
问题
在JAVA中调用本地函数两次会导致错误,因为这相当于连续调用C函数processRequest(char*, char*),而我的应用程序不支持这种操作。我希望每次都能像运行可执行文件两次一样独立地调用它。(当同一个函数在同一个应用程序中被调用两次时,程序无法正常工作)。我希望JNI在进行本地调用时重新初始化所有内容。我该如何做到这一点?