Bos*_*one 78
只需将它打包在罐子里的任何地方.有一件事你必须记住 - 在你可以使用DLL之前,你需要从JAR中实际提取这些并将它们转储到某个地方的硬盘上,否则你将无法加载这些
所以基本上 - 我为客户做了JNI项目,我将在战争中使用这样的jar包装.但是 - 在运行任何本机方法之前,我会将DLL作为资源并将其写入光盘到temp目录.然后我会运行常规初始化代码,其中我的DLL设置为我刚刚写入DLL的相同位置
哦,以防万一:将dll或任何其他文件打包到jar中没有什么特别之处.就像把东西包装成拉链一样
这是我刚刚挖掘出来的一些代码
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
}
Run Code Online (Sandbox Code Playgroud)
小智 6
使用http://www.jdotsoft.com/JarClassLoader.php可以从另一个具有无限嵌套的JAR加载DLL和JAR.例如,DLL可能位于另一个根JAR中的JAR中.所有DLL和JAR都像在类路径或库路径中一样加载.
归档时间: |
|
查看次数: |
77121 次 |
最近记录: |