org.netlib.utils
Class JNIMethods

java.lang.Object
  extended by org.netlib.utils.JNIMethods

public final class JNIMethods
extends java.lang.Object

Static helper methods for dealing with the Java Native Interface and detection of operating systems and architectures. The main purpose of this class is to provide a more portable alternative to the System.loadLibrary method (and horrible syntax of Bundle-NativeCode in MANIFEST files) when bundling binaries as part of a jar (either inside, or in a directory structure nearby).

Note on usage: unfortunately it is not possible to provide a loadLibrary(String) method here as it needs to know the calling class, which is lost when wrapped here. To load a native library using this class, call the getPortableLibraryName() method to return a String which may then be passed to System.loadLibrary().

Note: due to the nature of this class, a portable JUnit TestCase is not possible.


Nested Class Summary
static class JNIMethods.ARCHType
          Enumeration of common CPU architectures.
static class JNIMethods.OSType
          Enumeration of common operating systems, independent of version or architecture.
 
Field Summary
static JNIMethods.ARCHType ARCH
          Stores the CPU Architecture the JVM is currently running on.
static JNIMethods.OSType OS
          Stores the operating system the JVM is currently running on.
 
Constructor Summary
JNIMethods()
           
 
Method Summary
static java.lang.String getPortableLibraryName(java.lang.String name)
          The System.mapLibraryName method is broken in the sense that it does not give unique names for operating systems and architectures.
static void main(java.lang.String[] args)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ARCH

public static final JNIMethods.ARCHType ARCH
Stores the CPU Architecture the JVM is currently running on.


OS

public static final JNIMethods.OSType OS
Stores the operating system the JVM is currently running on.

Constructor Detail

JNIMethods

public JNIMethods()
Method Detail

getPortableLibraryName

public static java.lang.String getPortableLibraryName(java.lang.String name)
The System.mapLibraryName method is broken in the sense that it does not give unique names for operating systems and architectures. In order to facilitate cross-platform compatibility allowing bundled binary files, this method will return a unique library name, dependent on the operating system and architecture. The return value of this method should be passed to the System.loadLibrary method instead of the raw library name.

The resulting filename will be constructed by forcing the library name to lowercase and appending an extra String that is defined by the operating system (the architecture is only encoded if it is relevant). Some examples are for the parameter 'name':-

Parameters:
name -
Returns:
Throws:
java.lang.IllegalArgumentException - if the input name is zero length or contains a directory separator character.

main

public static void main(java.lang.String[] args)