在Java中加载.DLL

我想在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中添加了本地库位置中的文件夹path
  • Windows中更改%PATH%
  • 以绝对path为论据
  • 尝试使用“tttt.dll”,“./tttt.dll”和“.tttt.dll”
  • 调用System.loadLibrary(...)System.load(...)

UPDATE

我试图打印java.library.path并获得一个path。 我把dll放在这个path中,现在错误信息更加混乱:

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

以下是打印path的代码:

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