5,779,527 members and growing! (132 online)
Email Password   helpLost your password?
Languages » Java » General     Beginner License: The GNU General Public License (GPL)

Java Native Interface Auto Loader for JAR Deployment

By LiloHuang

In this tutorial, I will try to explain how to use JNI auto loader for JAR file.
Java, Java, JNI

Posted: 16 May 2008
Updated: 16 May 2008
Views: 1,871
Bookmarked: 2 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
2 votes for this Article.
Popularity: 1.05 Rating: 3.50 out of 5
0 votes, 0.0%
1
1 vote, 50.0%
2
0 votes, 0.0%
3
0 votes, 0.0%
4
1 vote, 50.0%
5
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 code

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

License

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

About the Author

LiloHuang


Lilo Huang was born in Tainan, Taiwan, Republic of China on March 14,1985. He received the B.S. degree in the Industrial Technology Education from National Kaohsiung Normal University in 2007. He is now a M.S student of the Department of Computer Science at the National Tsing Hua University. He current research interests are P2P System Design and Network Security.
Location: Taiwan Taiwan

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
  (Refresh) 
-- There are no messages in this forum --

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 16 May 2008
Editor:
Copyright 2008 by LiloHuang
Everything else Copyright © CodeProject, 1999-2009
Java | Advertise on the Code Project