Search code examples
javadlljava-native-interfaceshared-librariesnative

While loading JNI library, how the mapping happens with the actual library name


We load any native library by using

System.loadLibrary("hello")

Now I came to know that this library name refers to hello.dll for windows and libhello.so for unix based system.

So where these platform dependent changes happens?

Is JRE doing this?


Solution

  • tl;dr

    The platform dependent library name is built in native methods of the Java Virtual Machine. The actual algorithm simply prepends/appends a platform specific prefix/suffix to the name:

    • Windows: "hello" -> "hello.dll"
    • Linux/Solaris: "hello" "libhello.so"
    • Mac: "hello" -> "libhello.dylib"

    Long version:

    There are a couple of JDK Java methods which deal with loading libraries and/or library names:

    java.lang.System.loadLibrary(String name)
    java.lang.System.mapLibraryName(String name)
    java.lang.Runtime.loadLibrary(String name) 
    java.lang.ClassLoader.loadLibrary(String name)
    

    The famous System.loadLibrary actually calls Runtime.loadLibrary which calls ClassLoader.loadLibrary. In the end the implementations of these methods call the following native methods which translate the given library name into the platform specific name:

    native java.lang.System.mapLibraryName(String name)
    native java.lang.ClassLoader$NativeLibrary.findBuiltinLib(String name) 
    

    The implementations of these native methods can be found in (link to the OpenJDK versions):

    Both methods implement the same algorithm to build the actual library name, prepending the prefix JNI_LIB_PREFIX and appending the suffix JNI_LIB_SUFFIX.

    In the end the macros JNI_LIB_PREFIX and JNI_LIB_SUFFIX are defined in platform dependent include files, namely