简体   繁体   中英

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 :

  • Set arguments in Eclipse
  • Moving at root of project and System32 folder
  • Added the folder path in native library location in Eclipse
  • Changing the %PATH% in windows
  • Giving the absolute path as an argument
  • Trying with "tttt.dll", "./tttt.dll" and ".tttt.dll"
  • Call with System.loadLibrary(...) and System.load(...)

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());
}

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.

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);
 }
} 

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.

请尝试使用此代码:

System.loadLibrary("tttt");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM