繁体   English   中英

在Java中加载.DLL

[英]Loading .DLL in Java

我想在Eclipse中调用.DLL方法。 这是我的代码:

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

问题是我在编译时遇到这个错误:

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)

我已经尝试过:

  • 在Eclipse中设置参数
  • 移动项目和System32文件夹的根目录
  • 在Eclipse中的本机库位置添加了文件夹路径
  • 更改Windows中的%PATH%
  • 给出绝对路径作为论据
  • 尝试使用“tttt.dll”,“。/tttt.dll”和“.tttt.dll”
  • 使用System.loadLibrary(...)System.load(...)调用

UPDATE

我试图打印java.library.path并获取路径。 我将dll放在此路径中,错误消息现在更加混乱:

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

以下是打印路径的代码:

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

第一个问题是它无法找到dll,因为它不在路径中。

第二个问题是它无法找到您正在使用的dll上的依赖项。 你的选择似乎是

  1. 将所有依赖库复制到dll位置
  2. 从原始dll位置运行代码。

给出文件的绝对路径

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

sun.jna包中使用Library接口就可以了:

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

    }

}

还是不知道为什么以前没用过。

请尝试使用此代码:

System.loadLibrary("tttt");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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