使用java.library.path和LD_LIBRARY_PATH之间的区别 [英] Difference between using java.library.path and LD_LIBRARY_PATH

查看:500
本文介绍了使用java.library.path和LD_LIBRARY_PATH之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

设置JVM参数之间是否有区别

Is there a difference between setting the JVM argument

-Djava.library.path=/path 

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

at JVM start and setting the Linux environment variable

export LD_LIBRARY_PATH=/path 

这两种方法有哪些优点/缺点?

What are the advantages/disadvantages of the two approaches?

推荐答案

第一个表格

-Djava.library.path=/path

将在java字节码级别处理, System.loadLibrary 将调用 Runtime.loadLibary ,然后将调用 java / lang / ClassLoader.loadLibrary 。在函数调用 ClassLoader.loadLibrary 中,将检查系统属性 java.library.path 以获取完整路径的库并将此完整路径传递给本机代码以调用系统api dlopen / dlsym ,最终使库加载。您可以从 OpenJDK <浏览源代码/ a>存储库。以下代码片段是我从链接中复制的段。

will be handled in java bytecode level, System.loadLibrary will call Runtime.loadLibary, then will call java/lang/ClassLoader.loadLibrary. In the function call ClassLoader.loadLibrary, the system property java.library.path will be checked to get the full path of the library and pass this full path to native code to call system api dlopen/dlsym, eventually make the library loaded. You can browse the source from OpenJDK repository. The following code snippet is the segment I copy from the link.

此表单的优点是,如果存在Java代码,您将收到错误或警告或异常库路径存在一些问题。

The advantage of this form is that you will get error or warning or exception in Java code, if there are some problems with your library path.

// 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的实施来自此处

In this manner, if there are some problems with your library path and the system can't load your library, the system won't give too much clue what happen and will fail silently (I guess). It depends whether or not to implement LD_LIBRARY_PATH, Android didn't use LD_LIBRARY_PATH to determine the library location, you can see Android's implementation from here.

这篇关于使用java.library.path和LD_LIBRARY_PATH之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆