从JAR中提取和加载DLL [英] Extract and load DLL from JAR

查看:135
本文介绍了从JAR中提取和加载DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Java应用程序使用一个DLL库。如何从JAR文件中获取它?



该DLL位于项目的源文件夹中。我必须将其包含在我的JAR中,在运行时(在jar的同一个目录下)解压缩,并加载它。

解决方案

推荐)。所以你必须从jar中提取它并将其复制到lib路径中。

  private static void loadLib(String path,String name){
name = System.mapLibraryName(name); //扩展名为.dll,.so或.dylib
try {
InputStream in = ACWrapper.class.getResourceAsStream(/+ path + name);
文件fileOut = new File(你的lib路径);
OutputStream out = FileUtils.openOutputStream(fileOut);
IOUtils.copy(in,out);
in.close();
out.close();
System.load(fileOut.toString()); //加载到这里
} catch(异常e){
// handle
}
}

注意: ACWrapper 是持有静态方法的类


My Java application uses a DLL library. How can I get it work from the JAR file?

The DLL is in the project's sources folder. I have to include it in my JAR, extract it at runtime (in the same directory of the jar) and load it.

解决方案

You need to put dll in your library path (recommended ) before you try to load it. so that you will have to extract it from jar and copy it into lib path .

private static void loadLib(String path, String name) {
  name = System.mapLibraryName(name); // extends name with .dll, .so or .dylib
  try {
        InputStream in = ACWrapper.class.getResourceAsStream("/"+path + name);
        File fileOut = new File("your lib path");
        OutputStream out = FileUtils.openOutputStream(fileOut);
        IOUtils.copy(in, out);
        in.close();
        out.close();
        System.load(fileOut.toString());//loading goes here
   } catch (Exception e) {
               //handle
   }
}

Note: ACWrapper is the class holding the static method

这篇关于从JAR中提取和加载DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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