com.nullprogram.guide
Class NativeGuide

java.lang.Object
  extended by com.nullprogram.guide.NativeGuide

public final class NativeGuide
extends Object

This is a utility class for loading native libraries from the classpath. The JVM is unable to load libraries from inside .jar files. This class works around that by copying them out to a temporary directory on the filesystem. When using this utility class, imagine you are registering all versions of your native library. It would be used like this, for example,

 NativeGuide.prepare(Arch.LINUX_32, "/x86/libexample.so");
 NativeGuide.prepare(Arch.LINUX_64, "/amd64/libexample.so");
 NativeGuide.prepare(Arch.WINDOWS_32, "/x86/example.dll");
 NativeGuide.prepare(Arch.WINDOWS_64, "/amd64/example.dll");
 
Libraries not used by the running architecture will be ignored.


Method Summary
static Arch getArchitecture()
          Determine the JVM's native architecture.
static void load(Arch arch, String path)
          Load a native library resource by first copying it to a temporary directory.
static void load(String arch, String os, String path)
          Load a native library resource by first copying it to a temporary directory.
static void prepare(Arch arch, String path)
          Prepare a native library to be loaded by copying it to a temporary directory.
static void prepare(String arch, String os, String path)
          Prepare a native library to be loaded by copying it to a temporary directory.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

load

public static void load(Arch arch,
                        String path)
                 throws IOException
Load a native library resource by first copying it to a temporary directory. If the given architecture doesn't match the running system, nothing is done.

Parameters:
arch - the architecture of the library
path - the path to the library as a resource
Throws:
IOException - if the library doesn't exist or could not load

prepare

public static void prepare(Arch arch,
                           String path)
                    throws IOException
Prepare a native library to be loaded by copying it to a temporary directory. The directory is automatically added to java.library.path. If the given architecture doesn't match the running system, nothing is done.

Parameters:
arch - the architecture of the library
path - the path to the library as a resource
Throws:
IOException - if the library does not exist

load

public static void load(String arch,
                        String os,
                        String path)
                 throws IOException
Load a native library resource by first copying it to a temporary directory. If the given architecture doesn't match the running system, nothing is done. This method allows for non-predefined architectures.

Parameters:
arch - the os.arch string
os - the os.name string
path - the path to the library as a resource
Throws:
IOException - if the library doesn't exist or could not load

prepare

public static void prepare(String arch,
                           String os,
                           String path)
                    throws IOException
Prepare a native library to be loaded by copying it to a temporary directory. The directory is automatically added to java.library.path. If the given architecture doesn't match the running system, nothing is done. This method allows for non-predefined architectures.

Parameters:
arch - the os.arch string
os - the os.name string
path - the path to the library as a resource
Throws:
IOException - if the library does not exist

getArchitecture

public static Arch getArchitecture()
Determine the JVM's native architecture.

Returns:
the native architecture code