用于 JAR 部署的 Java 本地接口自动加载器






3.50/5 (2投票s)
在本教程中,我将尝试解释如何使用JNI自动加载器来加载JAR文件。
引言
Java Native Interface自动加载器是一种方便的解决方案,用于从JAR文件中加载JNI动态链接库。
Using the Code
ExtractFromJar
是一个可以自动提取资源作为JNI库的方法。执行自动加载器后,您就可以使用JNI库了。
try{
ExtractFromJar("test.dll");
}catch(IOException e) {}
System.load(System.getProperty("java.io.tmpdir") + "/test.dll");
ExtractFromJar 方法
public void ExtractFromJar(String name)
throws IOException {
File directory = new File(System.getProperty("java.io.tmpdir"));
if(!directory.exists())
directory.mkdirs();
File libFile = new File(directory, name);
if(libFile.exists())
libFile.delete();
InputStream in = getClass().getResourceAsStream("/" + name);
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(libFile));
byte buffer[] = new byte[1024];
int len;
for(int sum = 0; (len = in.read(buffer)) > 0; sum += len)
out.write(buffer, 0, len);
in.close();
out.close();
}
历史
- 2008年5月17日:初始发布