3

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 not appear in library name: C:\HelloWorld.dll

Any ideas how I can solve this?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Stefan
  • 14,826
  • 17
  • 80
  • 143

4 Answers4

4

Just use:

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

Also, make sure HelloWorld.dll is available on your library path.

Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292
  • 1
    ok, I'm trying it without C: and I am getting ther error: java.lang.UnsatisfiedLinkError: no HelloWorld.dll in java.library.path. How do I set the native path? I tried it in the properties -> Java Build Path -> Libraries, but its not working – Stefan Mar 22 '11 at 09:37
4

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.

tostao
  • 2,803
  • 4
  • 38
  • 61
Ralph Löwe
  • 415
  • 4
  • 10
0

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/

Kevin
  • 552
  • 2
  • 8
  • 17
-1

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.

eee
  • 1,043
  • 7
  • 5