javachromium-embedded

Why java.library.path is not working on ubuntu?


I am working on an java component which need some libraries i.e .dll for windows system and .so files for Linux system. So I prepared an java application in netbeans and added some dependency jars and build the project.

Execution on Windows:

When I tried to run jar file on Windows system using command java -jar appName.jar I got java.lang.UnsatisfiedLinkError so I specified java.library.path while execution like java -Djava.library.path=full\\path\\to\\libs -jar appName.jar and it got run successfully on Windows.

Execution on Linux(ubuntu) :

When I tried to execute same jar file on ubuntu with the same command java -Djava.library.path=/path/to/libs -jar appName.jar I got error saying some .so file are not found on specified location (I checked file location and permissions, all is ok)

Updated (added error):

ubuntu@ubuntu-HP-dx2480-MT-KL969AV:~/Desktop$ java -Djava.library.path=/home/ubuntu/Desktop/bin -jar JavaApplication4.jar

initialize on Thread[AWT-EventQueue-0,6,main] with library path bin bin/jcef_helper: error while loading shared libraries: libcef.so: cannot open shared object file: No such file or directory

contains of bin folder

-rwxr-xr-x 1 ubuntu ubuntu   1483321 Jun 18  2014 cef.pak
-rwxr-xr-x 1 ubuntu ubuntu   3258231 Jun 18  2014 devtools_resources.pak
-rwxr-xr-x 1 ubuntu ubuntu    971264 Jun 11  2014 ffmpegsumo.dll
-rwxr-xr-x 1 ubuntu ubuntu   9994752 Jun 11  2014 icudt.dll
-rwxr-xr-x 1 ubuntu ubuntu    429568 Jun 18  2014 jcef.dll
-rwxr-xr-x 1 ubuntu ubuntu    481488 Jun 18  2014 jcef_helper
-rwxr-xr-x 1 ubuntu ubuntu    233984 Jun 18  2014 jcef_helper.exe
-rwxr-xr-x 1 ubuntu ubuntu  53280768 Jun 11  2014 libcef.dll
-rwxr-xr-x 1 ubuntu ubuntu 105317136 Jun 18  2014 libcef.so
-rwxr-xr-x 1 ubuntu ubuntu   1309288 Jun 18  2014 libffmpegsumo.so
-rwxr-xr-x 1 ubuntu ubuntu   1047296 Jun 18  2014 libjcef.so drwxrwxrwx 2 ubuntu ubuntu      4096 Dec 23 11:29 locales

By some searching I come to know that I have try with LD_LIBRARY_PATH environment variable so I created an sh file having command:

export LD_LIBRARY_PATH=/path/to/libs

java -jar /path/to/appName.jar  

and when I run sh file my program runs successfully.

So my question is why java.library.path is not works for ubuntu (linux)? Is it like java.library.path is only for windows?


Solution

  • So my question is why java.library.path is not works for ubuntu (linux)? Is it like java.library.path is only for windows?

    It does work, we use it a lot. Start your application with -XshowSettings:properties and take a look at the search path for debugging.

    We usually deploy the libraries as a package to /usr/local/lib, since the libraries are often used by other components too. Don't forget to call ldconfig after placing a new library in there (so much for the export LD_LIBRARY_PATH part).

    As far as i remember it should be enough with just adding the folder with -Djava.library.path if i recall correctly. I will look into it and tell you later to clarify.

    Also please post readlink -f /home/ubuntu/Desktop/bin, file /home/ubuntu/Desktop/bin/libcef.so and ldd /home/ubuntu/Desktop/bin/libcef.so.

    Update: I will try to explain why things work and why not.

    Lets talk about java.library.path. This property is used by the VM for looking up libraries. Take a look at java.lang.System#load*(String libName) for reference. The java.library.path property has some paths pre-set, the following shows the output on my ubuntu box:

    ortang@vbox-devel:~$ java -XshowSettings:properties
    Property settings:
        ...
        java.library.path = /usr/java/packages/lib/amd64
            /usr/lib/x86_64-linux-gnu/jni
            /lib/x86_64-linux-gnu
            /usr/lib/x86_64-linux-gnu
            /usr/lib/jni
            /lib
            /usr/lib
    

    Be aware that using this property will overwrite the existing property.

    ortwin@vbox-devel:~$ java -Djava.library.path=/some/other/folder:/yet/another/one -XshowSettings:properties
    Property settings:
        ...
        java.library.path = /some/other/folder
            /yet/another/one
    

    So far so good. The JVM is looking only in the folders defined in that property!

    The libraries you make the JVM to load will most likely have dependencies to other libraries. Be aware that these dependencies are looked up by the operating system, just as any other shared library!

    So to solve your problem you have to make sure that the libraries you load have their dependencies resolved! Use ldd for debugging that matter.

    The LD_LIBRARY_PATH environment variable does a similar job, as it adds paths that will be used for library lookup by the OS. I am no fan of using it in production environments.