嗨,大家好我正在从C++ dll调用函数的一些java代码。但是可以正确调用dll的某些函数,而其他函数则不能。 我先写了一个java类,将所有的函数从dll中包装好,然后用javah生成相应的jni头文件。最后我写的C++代码包括生成的jni头文件。 C++文件是用Visual Studio编写的,而java代码是用Eclipse编写的。jni原生方法问题
下面是我的代码,我已经删除了一些不相关的代码。
爪哇:
public class VideoDetecion {
static {
System.load("dll_video_detect.dll");
System.load("vd_jni_impl.dll");
}
public static native int getFrame(String videoName, int second,String frameName);
public static native int getFrame1(String videoName);
public static native int getFrame2(String videoName, int second);
}
C++
using cv::VideoCapture;
using cv::Mat;
using std::string;
using std::bind;
using std::shared_ptr;
using std::vector;
using std::string;
using namespace std::placeholders;
JNIEXPORT jint JNICALL Java_videoDetectionJNI_VideoDetecion_getFrame1
(JNIEnv *env, jclass, jstring videoName)
{
//String videoName = "D:\\videos\\detect1\\0.mp4";
shared_ptr<const char> vn(env->GetStringUTFChars(videoName, NULL), bind(&JNIEnv::ReleaseStringUTFChars, env, videoName, _1));
int second = 10;
string frameName = "D:\\videos\\detect1\\0-10.jpg";
vd::getVideoFrame(string(vn.get()), second, frameName);
return 0;
}
/*
* Class: videoDetectionJNI_VideoDetecion
* Method: getFrame2
* Signature: (Ljava/lang/String;I)I
*/
JNIEXPORT jint JNICALL Java_videoDetectionJNI_VideoDetecion_getFrame2
(JNIEnv *env, jclass, jstring videoName, jint second)
{
shared_ptr<const char> vn(env->GetStringUTFChars(videoName, NULL), bind(&JNIEnv::ReleaseStringUTFChars, env, videoName, _1));
string frameName = "D:\\videos\\detect1\\0-10.jpg";
vd::getVideoFrame(string(vn.get()), second, frameName);
return 0;
}
JNIEXPORT jint JNICALL Java_videoDetectionJNI_VideoDetecion_getFrame
(JNIEnv *env, jobject, jstring videoName, jint second, jstring frameName)
{
shared_ptr<const char> vn(env->GetStringUTFChars(videoName, NULL), bind(&JNIEnv::ReleaseStringUTFChars,env, videoName,_1));
shared_ptr<const char> fn(env->GetStringUTFChars(frameName, NULL), bind(&JNIEnv::ReleaseStringUTFChars,env,frameName,_1));
if (videoName == NULL || frameName==NULL)
{
return -1;
}
vd::getVideoFrame(string(vn.get()), second, string(fn.get()));
return 0;
}
从蚀的错误信息是: 异常在线程 “主要” java.lang.UnsatisfiedLinkError中:videoDetectionJNI.VideoDetecion.getFrame(Ljava /郎/ String; ILjava/lang/String;)I at videoDetectionJNI.VideoDetecion.getFrame(Native Method) at videoDetectionJNI.Test.main(Test.java:48)
让我感到悲惨的是方法getFrame1和getFrame2正常工作,但我想要的真正的方法getFrame没有。此外,当我使用Visual Studio附加到进程java.exe来调试程序时,程序可以在cpp文件中的getFrame1和getFrame2函数中停止断点,但不会停止在getFrame函数的断点处。
somoeone可以帮助我吗?这真的让我感到困惑。
ps。我是新来的Java。
dumpbin/exports vd_jni_impl.dll显示什么?即您认为导出实际导出的符号?其次,你重新运行'javah'是因为最后一个函数的签名与在.java文件中运行它的内容不匹配(即使它放在'videoDetectionJNI'包中)。 – Petesh 2014-09-25 08:25:13
5 4 00001221 Java_videoDetectionJNI_VideoDetecion_getFrame0 = @ ILT + 540(Java_videoDetectionJNI_VideoDetecion_getFrame0) 6 5 00001226 Java_videoDetectionJNI_VideoDetecion_getFrame1 = @ ILT + 545(Java_videoDetectionJNI_VideoDetecion_getFrame1) 7 6 000Java_videoDetectionJNI_VideoDetecion_getFrame2 = @ ILT + 555(Java_videoDetectionJNI_VideoDetecion_getFrame2) – 2014-09-25 08:44:13
三个功能全部出口。我已经重新运行了javah(通过Eclipse),并且头文件是最新的 – 2014-09-25 08:45:56