栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用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的实现。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/380761.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号