Skip to content

adamheinrich/native-utils

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
June 14, 2015 10:04
April 20, 2016 13:22
October 24, 2017 22:13

Native Utils

A simple library class which helps with loading dynamic libraries stored in the JAR archive. These libraries usualy contain implementation of some methods in native code (using JNI - Java Native Interface).

Notes

  • The temporary file is stored into temp directory specified by java.io.tmpdir (by default it’s the operating system’s temporary directory). It should be automatically deleted when the application exits.
  • Although the code has some try-finally section (to be sure that streams are closed properly in case an exception is thrown), it does not catch exceptions. The exception has to be handled by the application. I belive this approach is cleaner and has some benefits.

Usage

To load the dynamic library, just make sure it is packed inside the JAR archive and call method loadLibraryFromJar:

import cz.adamh.NativeUtils;
 
public class HelloJNI {  
    static {   
        try {    
            NativeUtils.loadLibraryFromJar("/resources/libHelloJNI.so");   
        } catch (IOException e) {
            // This is probably not the best way to handle exception :-)    
            e.printStackTrace();
        }    
    }  
 
    public native void hello();    
}

More information

More information can be found in accompanying blog post.

About

A simple library class which helps with loading dynamic JNI libraries stored in the JAR archive

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages