[英]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)
我已经尝试过:
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上的依赖项。 你的选择似乎是
给出文件的绝对路径
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.