Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between using java.library.path and LD_LIBRARY_PATH

Is there a difference between setting the JVM argument

-Djava.library.path=/path 

at JVM start and setting the Linux environment variable

export LD_LIBRARY_PATH=/path 

before the JVM is started?

What are the advantages/disadvantages of the two approaches?

like image 500
christian Avatar asked Jan 14 '15 14:01

christian


People also ask

What is the difference between PATH and LD_LIBRARY_PATH?

PATH is for specifying directories of executable programs. LD_LIBRARY_PATH is used to specify directories of libraries. From other point of view, PATH is used primarily by the shell, while LD_LIBRARY_PATH is used by the dynamic loader (usually ld-linux.so ).

What is LD_LIBRARY_PATH used for?

The LD_LIBRARY_PATH environment variable tells Linux applications, such as the JVM, where to find shared libraries when they are located in a different directory from the directory that is specified in the header section of the program.

What is Java library PATH?

java. library. path is a System property, which is used by Java programming language, mostly JVM, to search native libraries, required by a project. Similar to PATH and Classpath environment variable, java.

Where is LD_LIBRARY_PATH in Windows?

If you are using windows and need to have that dll loaded, use the "PATH" system variable or drop the dll in the Windows/System32 directory. LD_LIBRARY_PATH is not used in windows.

How to get the full path of a library in Java?

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.

Why LD_LIBRARY_PATH is required in JVM?

If a library is not explcitely loaded from java, i.e. a dependent library has to be used, then LD_LIBRARY_PATH has to be used to have that library available in the jvm. Thanks for contributing an answer to Stack Overflow!

What is the difference between pythonpath and tcllibpath?

PYTHONPATH: Python libraries (e.g. /usr/local/lib/python:/usr/lib/python:/usr/lib/python2.6 ). TCLLIBPATH: TCL libraries (e.g. /usr/local/lib/tcltk:/usr/lib/tcltk ).

What is LD and perl5lib?

The name LD comes from dynamic loader, the system component that loads libraries into dynamically linked executables. PERL5LIB: Perl libraries (e.g. /usr/local/lib/site-perl:/usr/lib/site-perl:/usr/lib/perl:/usr/share/perl).


2 Answers

The first form

-Djava.library.path=/path

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.

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);
    }
// ....

The second form

export LD_LIBRARY_PATH=/path

will be handled in native, according to the document of 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.)

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.

like image 166
alijandro Avatar answered Oct 16 '22 17:10

alijandro


Java can explicitely load libaries listed with -Djava.library.path=... as described by alijandro.

For example, if mq series is used in bindings mode, the path for the neccessary libraries can be specified with -Djava.library.path=/opt/mq/java/lib and mqseries loads the libraries.

If a library is not explcitely loaded from java, i.e. a dependent library has to be used, then LD_LIBRARY_PATH has to be used to have that library available in the jvm.

like image 25
christian Avatar answered Oct 16 '22 19:10

christian