从JAR中提取并加载DLL

我的Java应用程序使用DL​​L库。 我怎样才能从JAR文件工作?

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

您需要将DLL放入您的库path( 推荐 ),然后再尝试加载它。 所以你将不得不从jar中提取它并将其复制到libpath。

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 } } 

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

 try { InputStream in = Main.class.getResourceAsStream("/example-input.dll"); File fileOut = new File("./example-output.dll"); DataOutputStream writer = new DataOutputStream(new FileOutputStream(fileOut)); long oneChar = 0; while((oneChar = in.read()) != -1){ writer.write((int)oneChar); } in.close(); writer.close(); } catch (Exception e) { e.printStackTrace(); }