Class JNIMethods

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

public final class JNIMethods extends 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.

  • Field Details

    • 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 Details

    • JNIMethods

      public JNIMethods()
  • Method Details

    • getPortableLibraryName

      public static String getPortableLibraryName(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':-

      • Apple (G3, G4): "name-apple-ppc"
      • Apple (G5): "name-apple-ppc_64"
      • Apple (Intel): "name-apple-x86"
      • Apple (Intel 64 Bit mode): "name-apple-x86_64"
      • Linux (i686): "name-linux-x86"
      • Linux (Intel/AMD 64): "name-linux-x86_64"
      • Linux (sparc): "name-linux-sparc"
      • Linux (PPC 32 bit): "name-linux-ppc"
      • Linux (PPC 64 bit): "name-linux-ppc_64"
      • Windows XP/Vista (i686): "name-windows-x86"
      • Windows XP/Vista (Intel/AMD 64): "name-windows-x86_64"
      • Sun Solaris (Blade): "name-sun-sparc"
      • Sun Solaris (Intel 64 bit): "name-sun-x86_64"
      Parameters:
      name -
      Returns:
      Throws:
      IllegalArgumentException - if the input name is zero length or contains a directory separator character.
    • main

      public static void main(String[] args)