我正在为工作编写一个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 (2010Express),并针对jvm.lib和jawt.lib进行链接,并静态地链接到MFC和ALT库。我错过什么了吗?
在构建DLL时,我应该提到以下输出:
创建库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> C:\Users\Alexander\youeye-rnd\java-rnd\libvpx-youeye\msvc\libvpx_ye\Debug\libvpx_ye.dll : 1>LINK :警告LNK4098: defaultlib 'LIBCMT‘与其他库冲突;使用/NODEFAULTLIB:library 1> libvpx_ye.vcxproj -> LNK4098
我尝试过将“忽略特定的默认库”(在Linker >Input下)设置为"/NODEFAULTLIB:libcmt“,但没有任何效果。我想这可能是我的问题,但我不完全确定。
发布于 2012-08-03 19:47:26
所以有两个问题使得这很难调试,但这两个问题都是我的错。
首先,当我创建最初的JNI c-头时,我没有在*.Java源代码中包含包名,因为我认为生成的DLL文件对什么包有点模糊,只要它了解它与其相关的函数。因此,我添加了正确的包名,在类上重做了javah,它用正确的包名重新生成了标头。
其次,为了解决msvc问题,在Properties > Linker > Input >“忽略特定的默认库”下,我在完整的命令行切换"/NODEFAULTLIB:libcmt“时,它只希望我放入libcmt并处理其余部分。一旦更正,它就在没有任何警告的情况下进行编译。
我希望这能帮助一些人在windows上调试自己的JNI/DLL问题。
https://stackoverflow.com/questions/11798440
复制相似问题