从JAR中提取并加载DLL

One*_*ros 15 java dll java-native-interface jar

我的Java应用程序使用DL​​L库.如何从JAR文件中获取它?

DLL位于项目的sources文件夹中.我必须将它包含在我的JAR中,在运行时提取它(在jar的同一目录中)并加载它.

Jig*_*shi 26

在尝试加载之前,需要将dll放在库路径中(推荐).所以你必须从jar中提取它并将其复制到lib路径中.

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
      }
    }
  }
Run Code Online (Sandbox Code Playgroud)

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