A portable statement to load a JNI library from another directory using a relative path?

Is there a platform-independent Java operator for loading my own library from a directory other than the Java source code? I would like to use something like this:

public class HelloWorld {
    static {
        System.loadLibrary("../some_project/HelloWorld");
    }

    public static native void print();
}

The problem is that System.loadLibrary () does not support directory separators in the pathname argument. In addition, System.load (), unfortunately, requires an absolute path name, which not only means that I cannot specify the relative directory as above (which I would like to do), but it also requires the argument to include, for example, the previous extension "lib" and ".so" in the name of the JNI library on a Linux system.

Is there a standard way to deal with this? If possible, I would like to avoid writing a bunch of platform-specific Java code to build the correct JNI library name.

+5
source share
2 answers

I believe that you are looking for the System.mapLibraryName , which is commonly used by the ClassLoader.findLibrary implementation. For instance:

File lib = new File("../some_project/" + System.mapLibraryName("HelloWorld"));
System.load(lib.getAbsolutePath());

This will use libHelloWorld.sofor Linux and HelloWorld.dllfor Windows. Keep in mind that some operating systems support multiple extensions, and mapLibraryName only supports one, by design. I know it's MacOS ( .dyliband .jnilibfor older) and AIX ( .aand .so).

+13
source

, , loadLibrary. , load. . , , , load. , , .

0

All Articles