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

小编典典

第一种形式

-Djava.library.path=/path

将在java字节码级别处理,System.loadLibrary先调用Runtime.loadLibary,然后再调用java/lang/ClassLoader.loadLibrary。在函数call中ClassLoader.loadLibrary,java.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的实现。

2020-06-03

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值