我想在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)
我已经试过:
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的依赖关系。 你的选择似乎是
给出文件的绝对路径
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");