Java本机库System.loadLibrary失败,出现UnsatisfiedLinkError错误
我试图在Java中使用本机C++库。Java本机库System.loadLibrary失败,出现UnsatisfiedLinkError错误
当我与
System.loadLibrary(filename);
加载它,我得到的错误:
java.lang.UnsatisfiedLinkError: Directory separator should not appear in library name: C:\HelloWorld.dll
任何想法如何,我可以解决这个问题?
只需使用:
System.loadLibrary("HelloWorld"); // without c:\ and without ".dll" extension
此外,还要确保HelloWorld.dll
可用你的库路径上。
我用JNA这样做...
JNA是一个简单的方法来调用本机的功能是提供NativeLibrary类可用来完成这项任务:
// 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);
}
令人惊讶的发现文档中,可以使用如下以及:
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());
它输出:
Proxy interface to Native Library <C:\workspace\jna\src\lib\[email protected]> - 0
的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.
如果我重新命名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.
更多通用howto:http://stackoverflow.com/que sints/14997343/how-to-call-external-dll-function-from-java-code – 2015-06-04 06:07:02