Java 如何制作包含 DLL 文件的 JAR 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1611357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make a JAR file that includes DLL files?
提问by Just a learner
I bought a third-party Java library which includes a JAR file and two DLL files. I wrote my own Java program which invoke the third-party JAR file. Now my question is how can I package all my code into a single JAR file which include all my code and the third-party JAR and DLLs?
我购买了一个第三方 Java 库,其中包括一个 JAR 文件和两个 DLL 文件。我编写了自己的 Java 程序来调用第三方 JAR 文件。现在我的问题是如何将我的所有代码打包到一个包含我所有代码以及第三方 JAR 和 DLL 的 JAR 文件中?
I know SWTis such a case. The swt.jar
includes dll files, but I don't know how to do this and how to make it work properly.
我知道SWT就是这种情况。在swt.jar
包括DLL文件,但我不知道如何做到这一点,以及如何使其正常工作。
采纳答案by Bostone
Just package it anywhere in the jar. One thing you have to keep in mind though - before you can use the DLLs you need to actually extract these from the JAR and dump these on the hard disk somewhere otherwise you won't be able to load these
只需将其包装在罐子中的任何位置即可。但是你必须记住一件事 - 在你可以使用 DLL 之前,你需要从 JAR 中实际提取这些并将它们转储到硬盘上的某个地方,否则你将无法加载这些
So basically - I did JNI project for the client where I will use such jar packaged within the war. However - before running any native methods I would get the DLL as a resource and write it to the disc into temp directory. Then I would run regular initialization code where my DLL is set to the same location I just wrote DLL to
所以基本上 - 我为客户端做了 JNI 项目,我将使用打包在战争中的此类 jar。但是 - 在运行任何本机方法之前,我会将 DLL 作为资源并将其写入磁盘到临时目录中。然后我会运行常规的初始化代码,其中我的 DLL 被设置为我刚刚写入 DLL 的相同位置
Oh, and just in case: there's nothing special about packaging dll or any other file into jar. It's just like packaging stuff into zip
哦,以防万一:将 dll 或任何其他文件打包到 jar 中没有什么特别之处。就像把东西打包成zip一样
Here's some code I just digged out
这是我刚刚挖出的一些代码
public class Foo {
private static final String LIB_BIN = "/lib-bin/";
private final static Log logger = LogFactory.getLog(ACWrapper.class);
private final static String ACWRAPPER = "acwrapper";
private final static String AAMAPI = "aamapi51";
private final static String LIBEAU = "libeay32";
static {
logger.info("Loading DLL");
try {
System.loadLibrary(ACWRAPPER);
logger.info("DLL is loaded from memory");
} catch (UnsatisfiedLinkError e) {
loadFromJar();
}
}
/**
* When packaged into JAR extracts DLLs, places these into
*/
private static void loadFromJar() {
// we need to put both DLLs to temp dir
String path = "AC_" + new Date().getTime();
loadLib(path, ACWRAPPER);
loadLib(path, AAMAPI);
loadLib(path, LIBEAU);
}
/**
* Puts library to temp dir and loads to memory
*/
private static void loadLib(String path, String name) {
name = name + ".dll";
try {
// have to use a stream
InputStream in = ACWrapper.class.getResourceAsStream(LIB_BIN + name);
// always write to different location
File fileOut = new File(System.getProperty("java.io.tmpdir") + "/" + path + LIB_BIN + name);
logger.info("Writing dll to: " + fileOut.getAbsolutePath());
OutputStream out = FileUtils.openOutputStream(fileOut);
IOUtils.copy(in, out);
in.close();
out.close();
System.load(fileOut.toString());
} catch (Exception e) {
throw new ACCoreException("Failed to load required DLL", e);
}
}
// blah-blah - more stuff
}
回答by gorokhmn
Use http://www.jdotsoft.com/JarClassLoader.phpwhich could load DLLs and JARs from another JAR with unlimited nesting. For example, DLL could be in JAR which is in another root JAR. All DLLs and JARs are loaded like they are in a classpath or library path.
使用http://www.jdotsoft.com/JarClassLoader.php可以从另一个具有无限嵌套的 JAR 加载 DLL 和 JAR。例如,DLL 可能位于另一个根 JAR 中的 JAR 中。所有 DLL 和 JAR 都像在类路径或库路径中一样加载。