Java 本机库 System.loadLibrary 因 UnsatisfiedLinkError 失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5388925/
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 10:54:03  来源:igfitidea点击:

Java native library System.loadLibrary fails with UnsatisfiedLinkError

javac++native

提问by Stefan

I'm trying to use a native C++ library in Java.

我正在尝试在 Java 中使用本机 C++ 库。

When I'm loading it with

当我加载它时

System.loadLibrary(filename);

I get the error:

我收到错误:

java.lang.UnsatisfiedLinkError: Directory separator should not appear in library name: C:\HelloWorld.dll

java.lang.UnsatisfiedLinkError:目录分隔符不应出现在库名中:C:\HelloWorld.dll

Any ideas how I can solve this?

有什么想法可以解决这个问题吗?

采纳答案by Pablo Santa Cruz

Just use:

只需使用:

System.loadLibrary("HelloWorld"); // without c:\ and without ".dll" extension

Also, make sure HelloWorld.dllis available on your library path.

另外,请确保HelloWorld.dll在您的库路径上可用。

回答by Ralph L?we

loadLibrary needs the filename without the path and the extension.

loadLibrary 需要没有路径和扩展名的文件名。

If you wanna use the full path, you can try the System.load() method.

如果你想使用完整路径,你可以尝试 System.load() 方法。

See java.lang.System API.

请参阅java.lang.System API

回答by Kevin

I used JNA to do that...

我用 JNA 来做到这一点......

JNA is a simple way to call Native functions it provide NativeLibrary class useful to accomplish this task:

JNA 是一种调用 Native 函数的简单方法,它提供了对完成此任务有用的 NativeLibrary 类:

//Java code to call a native function

//调用原生函数的Java代码

dll = NativeLibrary.getInstance(Mydll);

Function proxy;

proxy = dll.getFunction(Utils.getMethods().get("MyMethodEntryPoint"));
        byte result[] = new byte[256];
        int maxLen = 250;
        String strVer = "";
        Object[] par = new Object[]{result, maxLen};
        intRet = (Integer) proxy.invoke(Integer.class, par);
        if (intRet == 0) {
            strVer = Utils.byteToString(result);
        }

you can find documentation at http://jna.java.net/

您可以在http://jna.java.net/找到文档

回答by eee

Surprisingly, the following can be used as well:

令人惊讶的是,还可以使用以下内容:

    final File dll = new File("src/lib/Tester32.dll");

    Test32 test32 = (Test32) Native.loadLibrary(dll.getAbsolutePath(), Test32.class);

    System.out.println(test32.toString() + " - " + test32.GetLastError());

It outputs:

它输出:

Proxy interface to Native Library <C:\workspace\jna\src\lib\Tester32.dll@387842048> - 0

Javadoc says:

Javadoc 说:

loadLibrary

public static Object loadLibrary(String name, Class interfaceClass)

Map a library interface to the given shared library, providing the explicit interface class. If name is null, attempts to map onto the current process.

加载库

public static Object loadLibrary(String name, Class interfaceClass)

将库接口映射到给定的共享库,提供显式接口类。如果 name 为 null,则尝试映射到当前进程。

If I rename Tester32.dllin .\src\libfolder to something else, following exception will occur:

如果我重命名Tester32.dll.\src\lib文件夹到别的东西,会发生以下情况除外:

Exception in thread "main" java.lang.UnsatisfiedLinkError: Unable to load library 'C:\workspace\jna\src\lib\Tester32.dll': The specified module could not be found.

线程“main”中的异常 java.lang.UnsatisfiedLinkError:无法加载库 'C:\workspace\jna\src\lib\Tester32.dll':找不到指定的模块。