13
The first form
第一种形式
-Djava.library.path=/path
will be handled in java bytecode level, System.loadLibrary will call Runtime.loadLibary, then will call java/lang/ClassLoader.loadLibrary. In the function call ClassLoader.loadLibrary, the system property java.library.path will be checked to get the full path of the library and pass this full path to native code to call system api dlopen/dlsym, eventually make the library loaded. You can browse the source from OpenJDK repository. The following code snippet is the segment I copy from the link.
将在java字节码级别、系统中处理。loadLibrary将调用运行时。loadLibary,然后将调用java/lang/ClassLoader.loadLibrary。在函数中调用ClassLoader。loadLibrary,系统属性java.library。将检查path以获取库的完整路径,并将此完整路径传递到本机代码,以调用系统api dlopen/dlsym,最终使库被加载。您可以从OpenJDK存储库中浏览源代码。下面的代码片段是我从链接中复制的片段。
The advantage of this form is that you will get error or warning or exception in Java code, if there are some problems with your library path.
这种形式的优点是,如果您的库路径有问题,您将在Java代码中得到错误、警告或异常。
// Invoked in the java.lang.Runtime class to implement load and loadLibrary.
static void loadLibrary(Class fromClass, String name,
boolean isAbsolute) {
ClassLoader loader =
(fromClass == null) ? null : fromClass.getClassLoader();
if (sys_paths == null) {
usr_paths = initializePath("java.library.path");
sys_paths = initializePath("sun.boot.library.path");
}
if (isAbsolute) {
if (loadLibrary0(fromClass, new File(name))) {
return;
}
throw new UnsatisfiedLinkError("Can't load library: " + name);
}
// ....
The second form
第二种形式
export LD_LIBRARY_PATH=/path
will be handled in native, according to the document of dlopen/dlsym
根据dlopen/dlsym的文件,会在本地处理吗
dlopen()
The function dlopen() loads the dynamic library file named by the null-terminated string filename and returns an opaque "handle" for the
dynamic library. If filename is NULL, then the returned handle is for the main program. If filename contains a slash ("/"), then it is
interpreted as a (relative or absolute) pathname. Otherwise, the dynamic linker searches for the library as follows (see ld.so(8) for fur‐
ther details):
o (ELF only) If the executable file for the calling program contains a DT_RPATH tag, and does not contain a DT_RUNPATH tag, then the
directories listed in the DT_RPATH tag are searched.
o If, at the time that the program was started, the environment variable LD_LIBRARY_PATH was defined to contain a colon-separated list of
directories, then these are searched. (As a security measure this variable is ignored for set-user-ID and set-group-ID programs.)
In this manner, if there are some problems with your library path and the system can't load your library, the system won't give too much clue what happen and will fail silently (I guess). It depends whether or not to implement LD_LIBRARY_PATH, Android didn't use LD_LIBRARY_PATH to determine the library location, you can see Android's implementation from here.
通过这种方式,如果您的库路径有问题,并且系统无法加载您的库,那么系统将不会提供太多的线索,并且会无声地失败(我猜)。这取决于是否实现LD_LIBRARY_PATH, Android没有使用LD_LIBRARY_PATH来确定库的位置,从这里可以看到Android的实现。