如何制作包含DLL文件的JAR文件? [英] How to make a JAR file that includes DLL files?

查看:211
本文介绍了如何制作包含DLL文件的JAR文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我购买了第三方Java库,其中包含一个JAR文件和两个DLL文件。我写了自己的Java程序,它调用第三方JAR文件。现在我的问题是如何将我的所有代码打包成一个JAR文件,其中包括我的所有代码和第三方JAR和DLL?



<我知道SWT 是这种情况。 swt.jar 包括dll文件,但我不知道如何做到这一点,以及如何使其正常工作。

解决方案

只需将其包装在jar中的任何位置即可。有一件事你必须记住,在您可以使用DLL之前,您需要从JAR中实际提取这些,并将其转储到硬盘上,否则您将无法加载这些



所以基本上 - 我为客户端做了JNI项目,我将在战争中使用这样的jar包装。然而,在运行任何本机方法之前,我将把DLL作为一个资源,并将它写入光盘到temp目录。然后我将运行常规的初始化代码,其中我的DLL设置到相同的位置,我刚刚写DLL到



哦,以防万一:没有什么特别的包装dll或任何其他文件进入jar。这就像包装的东西进入zip



这里是我刚刚挖出的一些代码

  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(加载DLL);
try {
System.loadLibrary(ACWRAPPER);
logger.info(DLL从内存加载);
} catch(UnsatisfiedLinkError e){
loadFromJar();
}
}

/ **
*打包到JAR中时,提取DLL,将它们放入
* /
private static void loadFromJar (){
//我们需要将两个DLL都放到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;
尝试{
//必须使用流
InputStream in = ACWrapper.class.getResourceAsStream(LIB_BIN + name);
//总是写入不同的位置
文件fileOut = new File(System.getProperty(java.io.tmpdir)+/+ path + LIB_BIN + name);
logger.info(写dll到:+ 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 - 更多东西
}


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?

I know SWT is 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.

解决方案

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

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

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

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
}

这篇关于如何制作包含DLL文件的JAR文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆