[英]Java/JNI/MSVC java.lang.UnsatisfiedLinkError To my DLL Functions
我正在編寫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和顎文件.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”,但沒有任何效果。 我認為這可能是我的問題,但我不確定。
因此,有兩個問題使調試起來很困難,但這都是我的錯。
首先,當我制作原始的JNI c頭時,我沒有在* .Java源代碼中包含程序包名稱,因為我相信所生成的DLL文件與哪種程序包有點模棱兩可,只要它能夠理解該類是與功能有關。 因此,我在類上添加了正確的程序包名稱,redid javah,並使用正確的程序包名稱重新生成了標頭。
其次,要解決msvc問題,請在“屬性”>“鏈接器”>“輸入”>“忽略特定的默認庫”下,在只希望我將其放入libcmt的情況下,放入完整的命令行開關“ / NODEFAULTLIB:libcmt”其余的。 更正該錯誤后,它將在沒有任何警告的情況下進行編譯。
我希望這可以幫助某些人在Windows上調試自己的JNI / DLL問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.