Первая форма
-Djava.library.path=/path
будет обрабатываться в уровне байт-кода Java, System.loadLibrary
будет вызывать Runtime.loadLibary
, то будем называть java/lang/ClassLoader.loadLibrary
. В вызове функции ClassLoader.loadLibrary
будет проверено системное свойство java.library.path
, чтобы получить полный путь к библиотеке и передать этот полный путь к собственному коду, чтобы вызвать систему api dlopen/dlsym
, что в конечном итоге сделает библиотеку загруженной. Вы можете просмотреть источник из OpenJDK репозитория. Следующий фрагмент кода является копией сегмента I из ссылки.
Преимущество этой формы в том, что вы получите ошибку или предупреждение или исключение в коде 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);
}
// ....
Вторая форма
export LD_LIBRARY_PATH=/path
будет обрабатываться в родной, в соответствии с документом 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.)
Таким образом, если есть какие-то проблемы с вашей библиотеки путь, и система не может загрузить вашу библиотеку, система не даст слишком много подсказки, что произойдет, и будет терпеть неудачу (я думаю). Это зависит от того, следует ли реализовать LD_LIBRARY_PATH
, Android не использовал LD_LIBRARY_PATH
, чтобы определить местоположение библиотеки, вы можете увидеть реализацию Android от here.