我的Java应用程序使用了一个DLL库。我该如何让它从JAR文件中运行?
DLL文件在项目的源文件夹中。我需要将它包含在我的JAR文件中,在运行时提取它(放在与JAR文件相同的目录下)并加载它。
我的Java应用程序使用了一个DLL库。我该如何让它从JAR文件中运行?
DLL文件在项目的源文件夹中。我需要将它包含在我的JAR文件中,在运行时提取它(放在与JAR文件相同的目录下)并加载它。
在尝试加载dll之前,您需要将其放入库路径中(建议),这样您就必须从jar中提取它并将其复制到lib路径中。
private void loadLib(String path, String name) {
name = System.mapLibraryName(name); // extends name with .dll, .so or .dylib
InputStream inputStream = null;
OutputStream outputStream = null;
try {
inputStream = getClass().getResourceAsStream("/" + path + name);
File fileOut = new File("your lib path");
outputStream = new FileOutputStream(fileOut);
IOUtils.copy(inputStream, outputStream);
System.load(fileOut.toString());//loading goes here
} catch (Exception e) {
//handle
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
//log
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
//log
}
}
}
注意:ACWrapper
是持有静态方法的类。
C:\\windows\\system32
。 - jmj