Java native library System.loadLibrary fails with UnsatisfiedLinkError
I'm trying to use a native C++ library in Java.
When I'm loading it with
System.loadLibrary(filename);
I get the error:
java.lang.UnsatisfiedLinkError: Directory separator should no开发者_如何转开发t appear in library name: C:\HelloWorld.dll
Any ideas how I can solve this?
Just use:
System.loadLibrary("HelloWorld"); // without c:\ and without ".dll" extension
Also, make sure HelloWorld.dll
is available on your library path.
loadLibrary needs the filename without the path and the extension.
If you wanna use the full path, you can try the System.load() method.
See java.lang.System API.
I used JNA to do that...
JNA is a simple way to call Native functions it provide NativeLibrary class useful to accomplish this task:
//Java code to call a native function
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/
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:
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.
If I rename Tester32.dll
in .\src\lib
folder to something else, following exception will occur:
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.
精彩评论