Click here to Skip to main content
Licence GPL3
First Posted 16 May 2008
Views 10,294
Bookmarked 9 times

Java Native Interface Auto Loader for JAR Deployment

By | 16 May 2008 | Article
In this tutorial, I will try to explain how to use JNI auto loader for JAR file.

Introduction

The Java Native Interface Auto Loader is a convenience solution for loading JNI dynamic link library from JAR file.

Using the Code

ExtractFromJar is a method which can auto extract the resource as a JNI library. After executing 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 Method

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();
}

History

  • 17th May, 2008: Initial post

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

About the Author

Alice Huang

Other

Hong Kong Hong Kong

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 17 May 2008
Article Copyright 2008 by Alice Huang
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid