使用java.library.path和LD_LIBRARY_PATH之间的区别

设置JVM参数之间有区别吗

-Dlibrary.path" title="java.library.path">java.library.path=/path

在JVM启动并设置Linux环境变量时

export LD_LIBRARY_PATH=/path

JVM启动之前?

两种方法的优点/缺点是什么?

回答:

-Djava.library.path=/path

将在java字节码级别处理,System.loadLibrary先调用Runtime.loadLibary,然后再调用java/lang/ClassLoader.loadLibrary。在函数call中ClassLoader.loadLibraryjava.library.path将检查system属性以获取库的完整路径,并将此完整路径传递给本机代码以调用system

api

dlopen/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的实现。

以上是 使用java.library.path和LD_LIBRARY_PATH之间的区别 的全部内容, 来源链接: utcz.com/qa/433470.html

回到顶部