java dll jar_Java如何制作包含DLL文件的JAR文件?

小编典典

只需将其包装在jar中的任何位置即可。不过,你必须记住一件事-在使用DLL之前,你需要先从JAR中提取这些DLL,然后将其转储到硬盘上的某个位置,否则你将无法加载这些DLL。

所以基本上-我为客户端做了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

}

2020-03-05

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值