开发者

Extract and load DLL from JAR

开发者 https://www.devze.com 2023-02-05 21:03 出处:网络
My Java application uses a DLL library. How can I get it work from the JAR 开发者_C百科file? The DLL is in the project\'s sources folder. I have to include it in my JAR, extract it at runtime (in the

My Java application uses a DLL library. How can I get it work from the JAR 开发者_C百科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 void loadLib(String path, String name) {
  name = System.mapLibraryName(name); // extends name with .dll, .so or .dylib
  InputStream inputStream = null;
  OutputStream outputStream = null;
  try {
    inputStream = getClass().getResourceAsStream("/" + path + name);
    File fileOut = new File("your lib path");
    outputStream = new FileOutputStream(fileOut);
    IOUtils.copy(inputStream, outputStream);

    System.load(fileOut.toString());//loading goes here
  } catch (Exception e) {
    //handle
  } finally {
    if (inputStream != null) {
      try {
        inputStream.close();
      } catch (IOException e) {
        //log
      }
    }
    if (outputStream != null) {
      try {
        outputStream.close();
      } catch (IOException e) {
        //log
      }
    }
  }

}

Note: ACWrapper is the class holding the static method

0

精彩评论

暂无评论...
验证码 换一张
取 消