2015-11-11 48 views
3

我想在Eclipse中调用.DLL方法。这里是我的代码:在Java中加载.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文件夹
  • 在本机库loca中添加了文件夹路径重刑在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()); 
} 
+0

如果你指定一个绝对路径到你的DLL,例如'C:/ tttt.dll',它会工作吗?你有没有尝试过以下所有的例子:https://www.chilkatsoft.com/java-loadLibrary-Windows.asp – Cyclonecode

+0

当你指定一个绝对路径时,你会得到相同的错误信息**'C:/ tttt.dll'? – Cyclonecode

+0

是的,我也试过 – Totem

回答

0

使用在sun.jnaLibrary界面做的伎俩:

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

    } 

} 

还是不知道它为什么没有工作过。

-1

尝试使用这个:

System.loadLibrary("tttt"); 
+0

仍然gettint'java.lang.UnsatisfiedLinkError',谢谢你试试Dizzy。 – Totem

+0

我不认为这是无效的答案,即使它没有解决图腾的问题。 – Dizzy

+0

要查找tttt.dll的依赖库,必须使用Dependency Viewer(可下载)。 另一件事:希望你正在使用64位JDK(不能在原始帖子中提问)。 – Dizzy

0

给出文件的绝对路径

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

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

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

  1. 所有依赖库复制到DLL位置
  2. 从原来的DLL位置运行代码。
+0

请阅读我的帖子,关于我已经尝试过的。 – Totem