Class 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

      Nested Classes 
      Modifier and Type Class Description
      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

      Fields 
      Modifier and Type Field Description
      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

      Constructors 
      Constructor Description
      JNIMethods()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      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':-

        • 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:
        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)