只需将其包装在jar中的任何位置即可。不过,你必须记住一件事-在使用DLL之前,你需要先从JAR中提取这些DLL,然后将其转储到硬盘上的某个位置,否则你将无法加载这些DLL。
所以基本上-我为客户端做了JNI项目,我将在战争中使用这种打包的jar。但是-在运行任何本机方法之前,我将获得DLL作为资源并将其写入光盘到temp目录中。然后,我将运行常规的初始化代码,在该代码中,我的DLL设置为与我刚刚写入DLL相同的位置
哦,以防万一:将dll或任何其他文件包装到jar中没有什么特别的。就像把东西包装成拉链
这是我刚挖出的一些代码
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(); }}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);}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}


