java jna 加载库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6814728/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 17:22:37  来源:igfitidea点击:

jna load library

javadllloadjna

提问by Roland85

i'm having a problem on loading a dll with jna. While i can debug the code within eclipse, i get an Exception when i export and run it as a jar file:

我在用 jna 加载 dll 时遇到问题。虽然我可以在 eclipse 中调试代码,但当我将它作为 jar 文件导出并运行时,我得到一个异常:

java.lang.UnsatisfiedLinkError: unable to load library 'SiUSBXp'

java.lang.UnsatisfiedLinkError:无法加载库“SiUSBXp”

Any ideas why it does not find my dll when i run it as a jar-file?

当我将它作为 jar 文件运行时,为什么它找不到我的 dll 有什么想法吗?

Thanks!!!!

谢谢!!!!

public interface SiUSBXp extends StdCallLibrary {
    byte SI_GetNumDevices(IntByReference numdevices);
    byte SI_GetProductString( int deviceNum, byte[] productString, int options );
    byte SI_Open(IntByReference numdevices);
}

static SiUSBXp INSTANCE;

public static void main(String[] args) {
    System.setProperty("jna.library.path","SiUSBXp.dll");
    HashMap<String, StdCallFunctionMapper> optionMap = new HashMap<String, StdCallFunctionMapper>();
    StdCallFunctionMapper myMapper = new StdCallFunctionMapper();
    optionMap.put(Library.OPTION_FUNCTION_MAPPER, myMapper);
    INSTANCE = (SiUSBXp) Native.loadLibrary("SiUSBXp", SiUSBXp.class, optionMap);
}

------------------ EDIT ----------------------

- - - - - - - - - 编辑 - - - - - - - - - - -

The dll is located in the same folder as the jar is

dll 与 jar 位于同一文件夹中

------------------ EDIT 2 ---------------------

------------------ 编辑 2 ---------------------

I've just tried to run it within windows xp and it works .. but it does not within windows 7 (64 bit)

我刚刚尝试在 windows xp 中运行它并且它可以工作..但它不在 windows 7(64 位)中

------------------ EDIT 3 --------------------- I've solved the problem ... It was due to the java versions installed ... I removed them all, and then only installed the x86 version of java ... after that it worked perfectly

------------------ 编辑 3 --------------------- 我已经解决了这个问题......这是由于安装了java版本......我把它们都删除了,然后只安装了x86版本的java......之后它完美地工作

回答by chance

Put the DLL into the "Current Folder" or system variable PATH, or use -Djna.library.path=(path to the dll)as VM switch instead of hard-coding.

将 DLL 放入“当前文件夹”或系统变量 PATH,或-Djna.library.path=(path to the dll)用作 VM 开关而不是硬编码。

回答by Denis Tulskiy

I've had strange problems with jna 3.3.0 not finding libraries, try jna 3.2.7. Also, besides jna.library.path, it could help to set java.library.pathtoo. And I'm not sure if you should put library name in the path and not the folder the library is in. Try to set it to ".".

我在 jna 3.3.0 找不到库时遇到了奇怪的问题,试试 jna 3.2.7。此外,除此之外jna.library.path,它也可以帮助设置java.library.path。而且我不确定您是否应该将库名称放在路径中而不是库所在的文件夹中。尝试将其设置为“.”。