我正在为工作编写一个 libvpx 的视频编码器包装器,但是在 Java 中,当我尝试调用这些函数时,我得到 java.lang.UnsatisfiedLinkError。
这是我的Java代码:
package default;
class YE_Vpx {
native int create_stream( String path, int w, int h, int fps );
native void finalize_stream( int streamid );
native void append_stream( int streamid, int[] pixels );
native void finalize_streams( );
static {
System.loadLibrary("libvpx_ye"); // This loads the DLL just fine (windows 7), otherwise it would tell me it wasn't in the java.library.path
}
}
这是我的 C 标头(由 javah 生成):
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class YE_Vpx */
#ifndef _Included_YE_Vpx
#define _Included_YE_Vpx
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: YE_Vpx
* Method: create_stream
* Signature: (Ljava/lang/String;III)I
*/
JNIEXPORT jint JNICALL Java_YE_1Vpx_create_1stream
(JNIEnv *, jobject, jstring, jint, jint, jint);
/*
* Class: YE_Vpx
* Method: finalize_stream
* Signature: (I)V
*/
JNIEXPORT void JNICALL Java_YE_1Vpx_finalize_1stream
(JNIEnv *, jobject, jint);
/*
* Class: YE_Vpx
* Method: append_stream
* Signature: (I[I)V
*/
JNIEXPORT void JNICALL Java_YE_1Vpx_append_1stream
(JNIEnv *, jobject, jint, jintArray);
/*
* Class: YE_Vpx
* Method: finalize_streams
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_YE_1Vpx_finalize_1streams
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
这是我的 C 代码(它引用了我认为无法在此处展示的其他文件):
#include <jni.h>
#include <jni_md.h>
#include <sys/types.h>
#include "ye_vpx.h" // This is the javah generated header
#include "ye_vpx_c.h" // This is where most of the meat is, I can't actually post this file =/
#ifdef __cplusplus
extern "C" {
#endif
JNIEXPORT jint JNICALL Java_YE_1Vpx_create_1stream( JNIEnv *env, jobject obj, jstring path, jint w, jint h, jint fps )
{
jboolean iscopy;
const jchar *m_path = (*env)->GetStringChars(env, path, &iscopy);
jint ret = ye_vpx_create_stream( (const char *)m_path, w, h, fps );
(*env)->ReleaseStringChars(env, path, m_path);
return ret;
}
JNIEXPORT void JNICALL Java_YE_1Vpx_finalize_1stream(JNIEnv *env, jobject obj, jint streamid)
{
ye_vpx_finalize_stream( streamid );
}
JNIEXPORT void JNICALL Java_YE_1Vpx_append_1stream(JNIEnv *env, jobject obj, jint streamid, jintArray pixels)
{
jint *px = NULL;
int length = 0;
length = (*env)->GetArrayLength(env, pixels);
px = (jint *)calloc( length, sizeof(jint) );
(*env)->GetIntArrayRegion(env, pixels, 0, length, px);
//px = (jint *)GetIntArrayElements( env, pixels, &iscopy );
ye_vpx_append_stream( streamid, px );
free( px );
}
JNIEXPORT void JNICALL Java_YE_1Vpx_finalize_1streams(JNIEnv *env, jobject obj)
{
ye_vpx_finalize_streams();
}
#ifdef __cplusplus
}
#endif
据我所知,我已经正确导出了所有必要的内容。我正在使用 Microsoft Visual C (2010 Express),并且正在链接 jvm.lib 和jawt.lib,并且正在静态链接到 MFC 和 ALT 库。我错过了什么吗?
我应该在构建我的 DLL 时提到,我得到以下输出:
1> 创建库 C:\Users\Alexander\youeye-rnd\java-rnd\libvpx-youeye\msvc\libvpx_ye\Debug\libvpx_ye.lib 和对象 C:\Users\Alexander\youeye-rnd\java-rnd\libvpx -youeye\msvc\libvpx_ye\Debug\libvpx_ye.exp
1>LINK : 警告 LNK4098: defaultlib 'LIBCMT' 与其他库的使用冲突;使用 /NODEFAULTLIB:library 1> libvpx_ye.vcxproj -> C:\Users\Alexander\youeye-rnd\java-rnd\libvpx-youeye\msvc\libvpx_ye\Debug\libvpx_ye.dll
我尝试将“忽略特定默认库”(在链接器>输入下)设置为“/NODEFAULTLIB:libcmt”,但没有效果。我认为这可能是我的问题,但我不确定。