0xmax
0xmax

Reputation: 543

Loading .DLL in Java

I want to call a .DLL method in Eclipse. Here is my code :

class TestJNI1 {
          public native void LireNoVersion();
          public void a() {
              System.loadLibrary("tttt.dll");
              LireNoVersion();
          }

        }

    public GlobalAction() {
        this.setBackground(GlobalPreferences.PANEL_BACKGROUND_COLOR);
        new TestJNI1().a();
    }

The problem is that I have this error on compilation :

Exception in thread "AWT-EventQueue-0" java.lang.UnsatisfiedLinkError: Expecting an absolute path of the library: tttt.dll  at java.lang.Runtime.load0(Unknown Source)  at java.lang.System.load(Unknown Source)

I already tried to :

UPDATE

I tried to print the java.library.path and get a path. I put the dll in this path and the error message is more confusing now :

Exception in thread "AWT-EventQueue-0" java.lang.UnsatisfiedLinkError: D:\My\Exact\Path\tttt.dll: Can't find dependent libraries

Here is the code to print the path :

String property = System.getProperty("java.library.path");
StringTokenizer parser = new StringTokenizer(property, ";");
while (parser.hasMoreTokens()) {
    System.err.println(parser.nextToken());
}

Upvotes: 3

Views: 4109

Answers (4)

0xmax
0xmax

Reputation: 543

Using the Library interface in the sun.jna package did the trick :

  import com.sun.jna.Library;
  import com.sun.jna.Native;

  public class DllTest {
  public interface IDLL extends Library {
        IDLL INSTANCE = (simpleDLL) Native.loadLibrary("tttt", IDLL.class);

        int LireNoVersion(); // DWORD LireNoVersion();
    }

    public static void main(String[] args) {
        IDLL sdll = IDLL.INSTANCE;

        int nover = sdll.LireNoVersion();

        System.out.println("version = " + nover + "\n");

    }

}

Still don't know why it didn't worked before.

Upvotes: 0

Mike Murphy
Mike Murphy

Reputation: 1046

The first problem was it couldn't find the dll because it wasn't in the path.

The second problem is that it can't find the dependencies on the dll that you are using. Your choices would seem to be

  1. Copy all dependent libraries to the dll location
  2. Run your code from the original dll location.

Upvotes: 3

Kartik Ohri
Kartik Ohri

Reputation: 325

Give the absolute path of the file

 try {
    System.load("C:/myapplication/application.dll");
 } catch (UnsatisfiedLinkError e) {
  System.err.println("Native code library failed to load.\n" + e);
  System.exit(1);
 }
} 

Upvotes: 0

Dizzy
Dizzy

Reputation: 1

Try using this instead:

System.loadLibrary("tttt");

Upvotes: -1

Related Questions