设置JVM参数和设置系统属性有什么区别?
-Djava.library.path=/path
在 JVM 启动时设置 Linux 环境变量
export LD_LIBRARY_PATH=/path
JVM启动前有什么优缺点?
这两种方法各有何优劣之处?
设置JVM参数和设置系统属性有什么区别?
-Djava.library.path=/path
在 JVM 启动时设置 Linux 环境变量
export LD_LIBRARY_PATH=/path
JVM启动前有什么优缺点?
这两种方法各有何优劣之处?
第一种形式
-Djava.library.path=/path
该操作将在Java字节码级别处理,System.loadLibrary
将调用Runtime.loadLibary
,然后将调用java/lang/ClassLoader.loadLibrary
。在函数调用ClassLoader.loadLibrary
中,将检查系统属性java.library.path
以获取库的完整路径,并将此完整路径传递给本地代码来调用系统APIdlopen/dlsym
,最终使库加载。您可以从OpenJDK存储库中浏览源代码。以下代码片段是我从链接中复制的。
这种形式的优点是,如果您的库路径存在问题,您将在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的实现。-Djava.library.path=...
来显式加载列出的库,如alijandro所述。-Djava.library.path=/opt/mq/java/lib
指定必要库的路径,并且mqseries将加载这些库。LD_LIBRARY_PATH
使该库在jvm中可用。
java.library.path
中找到所引用的库,但会在$LD_LIBRARY_PATH
中找到它们。 - Rob I