|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
Introduction The Java Native Interface Auto Loader is a covenience solution for load JNI dynamic link library from JAR file Using the codeExtractFromJar is a method which can auto extract the resource as a JNI library. After excute the autoloader, you can enjoy the JNI library. try{
ExtractFromJar("test.dll");
}catch(IOException e) {}
System.load(System.getProperty("java.io.tmpdir") + "/test.dll");
The ExtractFromJar Methodpublic 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();
}
|
||||||||||||||||||||||