public final class NativeLibraryUtilities
extends java.lang.Object
loadNativeLibrary(String)
.
A native shared library can be provided to your program either as a file on the file system or as a resource in a jar file that is on the classpath of your program. In the later case the resources will be copied to a file before being loaded. This utility class tries the following approaches (in this order):
native.libpath.<libname>
for each library <libname>
. The name of the library
needs to be fully specified, e.g.
-Dnative.libpath.<libname>=/home/joe/java/native/<some_shared_library_file>The file has to exist or else
loadNativeLibrary(String)
will throw an exception.#getCompatibleComputerPlatform()
). The root of the hierarchy is given by the property
-Dnative.libpath=/home/joe/java/native/A real world example is the native library
nativedata
which has the hierarchy:
nativedata nativedata/arm-Linux nativedata/arm-Linux/libnativedata.so nativedata/i386-Linux nativedata/i386-Linux/libnativedata.so nativedata/amd64-Linux nativedata/amd64-Linux/libnativedata.so nativedata/i386-Mac OS X nativedata/i386-Mac OS X/libnativedata.jnilib nativedata/x86_64-Mac OS X nativedata/x86_64-Mac OS X/libnativedata.jnilib nativedata/x86-SunOS nativedata/x86-SunOS/libnativedata.so nativedata/amd64-SunOS nativedata/amd64-SunOS/libnativedata.so nativedata/sparc-SunOS nativedata/sparc-SunOS/libnativedata.so nativedata/sparcv9-SunOS nativedata/sparcv9-SunOS/libnativedata.so nativedata/x86-Windows nativedata/x86-Windows/nativedata.dll nativedata/amd64-Windows nativedata/amd64-Windows/nativedata.dllThe file has to exist or else
loadNativeLibrary(String)
will throw an exception.
native.caching.libpath.<libname>
instead of native.libpath.<libname>
, then you
will get the behavior of 1, but the library file on the file system will be considered a cache of a resource inside one of the jar files in the
class path. Inside of the jar file, the resources need ot have the structure as explained in 2.
The file does not have to exist as it will be unpacked from the appropriate jar file resource.
native.caching.libpath
instead of native.libpath
, then you will get the behavior of 2, but
the library files on the file system will all be considered a cache of a resource inside one of the jar files in the class path with the same
hierarchical structure.
The file does not have to exist as it will be unpacked from the appropriate jar file resource.
loadNativeLibrary(String)
will unpack the appopriate shared library
into a temporary directory and load it from there. The temporary file will be deleted at shutdown of the program (except on Microsoft Windows where
mandatory locks make this impossible). This is the 'auto mode' and thus the simplest way of using this utility class for the user of your
program.loadNativeLibrary(String)
will fall back to the Java default method of
loading JNI libraries via System.loadLibrary(String)
. This may require the Java property java.library.path
to be set and it may
require the library to follow a platform specific naming convention for the native shared library file.Constructor and Description |
---|
NativeLibraryUtilities() |
Modifier and Type | Method and Description |
---|---|
static boolean |
loadNativeLibrary(java.lang.String libraryName)
Loads the native library libraryName.
|
static java.lang.String |
tryCopyNativeLibraryToTempFile(java.lang.String libraryName,
boolean verbose)
Tries to copy a native library which is available as a resource to a temporary file.
|
public static boolean loadNativeLibrary(java.lang.String libraryName)
NativeLibraryUtilities
.libraryName
- The name of the native library to be loaded.true
if the library has been loaded successfully and false
otherwise.public static java.lang.String tryCopyNativeLibraryToTempFile(java.lang.String libraryName, boolean verbose)
/native/<libraryName>/<platform_id>/<libraryName>.so
.
libraryName
- The name of the library.verbose
- If true
, print error to stderr
if copying fails.null
, if the resource could not be copied.