简体   繁体   English

Java / JNI / MSVC java.lang.UnsatisfiedLinkError到我的DLL函数

[英]Java/JNI/MSVC java.lang.UnsatisfiedLinkError To my DLL Functions

I'm writing a video encoder wrapper of libvpx for work, but in Java, when I try and call these functions, I get java.lang.UnsatisfiedLinkError. 我正在编写libvpx的视频编码器包装,但是在Java中,当我尝试调用这些函数时,得到了java.lang.UnsatisfiedLinkError。

Here is my Java code: 这是我的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
    }
}

Here is my C Header (generated by javah): 这是我的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

And here is my C code (which references other files that I don't think I can present here): 这是我的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

As far as I'm aware, I've exported everything necessary and properly. 据我所知,我已经导出了所有必要且正确的内容。 I'm using Microsoft Visual C (2010 Express), and am linking against jvm.lib and jawt.lib, and am statically linking against the MFC and ALT libraries. 我正在使用Microsoft Visual C(2010 Express),并针对jvm.lib和颚文件.lib进行链接,并针对MFC和ALT库进行静态链接。 Did I miss anything? 我有想念吗?

I should mention while building my DLL, I get the following output: 我应该在构建DLL时提到以下输出:

1> Creating library C:\\Users\\Alexander\\youeye-rnd\\java-rnd\\libvpx-youeye\\msvc\\libvpx_ye\\Debug\\libvpx_ye.lib and object 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.lib和对象C:\\ Users \\ Alexander \\ youeye-rnd \\ java-rnd \\ libvpx -youeye \\ msvc \\ libvpx_ye \\ Debug \\ libvpx_ye.exp

1>LINK : warning LNK4098: defaultlib 'LIBCMT' conflicts with use of other libs; 1> LINK:警告LNK4098:defaultlib'LIBCMT'与使用其他库冲突; use /NODEFAULTLIB:library 1> libvpx_ye.vcxproj -> C:\\Users\\Alexander\\youeye-rnd\\java-rnd\\libvpx-youeye\\msvc\\libvpx_ye\\Debug\\libvpx_ye.dll 使用/ NODEFAULTLIB:library 1> libvpx_ye.vcxproj-> C:\\ Users \\ Alexander \\ youeye-rnd \\ java-rnd \\ libvpx-youeye \\ msvc \\ libvpx_ye \\ Debug \\ libvpx_ye.dll

I have tried setting "Ignore Specific Default Libraries" (under Linker > Input) to "/NODEFAULTLIB:libcmt" which had no effect. 我尝试将“忽略特定的默认库”(在“链接器”>“输入”下)设置为“ / NODEFAULTLIB:libcmt”,但没有任何效果。 I think this may be my issue, but I'm not absolutely sure. 我认为这可能是我的问题,但我不确定。

So there were two issues that made this difficult to debug, but both were sort of my fault. 因此,有两个问题使调试起来很困难,但这都是我的错。

First, when I made my original JNI c-header, I did not include the package name in the *.Java source, as I believed that the generated DLL file was sort of ambiguous to what package, as long as it understood the class it's functions related to. 首先,当我制作原始的JNI c头时,我没有在* .Java源代码中包含程序包名称,因为我相信所生成的DLL文件与哪种程序包有点模棱两可,只要它能够理解该类是与功能有关。 So, I added the proper package name, redid javah on the class, and it regenerated the header with the proper package name. 因此,我在类上添加了正确的程序包名称,redid javah,并使用正确的程序包名称重新生成了标头。

Second, to resolve the msvc issue, under Properties > Linker > Input > "Ignore Specific Default Libraries" I was putting in the full command line switch "/NODEFAULTLIB:libcmt" when it was only expecting me to put in libcmt and it took care of the rest. 其次,要解决msvc问题,请在“属性”>“链接器”>“输入”>“忽略特定的默认库”下,在只希望我将其放入libcmt的情况下,放入完整的命令行开关“ / NODEFAULTLIB:libcmt”其余的。 Once that was corrected, it compiled without any warnings. 更正该错误后,它将在没有任何警告的情况下进行编译。

I hope this helps some people debug their own JNI/DLL issues on windows. 我希望这可以帮助某些人在Windows上调试自己的JNI / DLL问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM