栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Java如何制作包含DLL文件的JAR文件?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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();    }}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);}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}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/441796.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号