54

I am working with a java application which needs a .dll file in java.library.path in windows. To run same application in Linux I have the respective .so file which should be added to java.library.path in linux machine, I didnt find any easy solution for this so far

I did put the .so in a folder which is already in the class path, but my application still complains there is no required .so file in java.library.path

I'd like to find:

  1. Ways to add .so to java.library.path
  2. How to know if its already added (when added)
nickhar
  • 19,981
  • 12
  • 60
  • 73
Lihkinisak
  • 749
  • 2
  • 7
  • 14

5 Answers5

82

Add the containing directory to LD_LIBRARY_PATH before launching the application

        export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/some/pathOfContainingDirectory

Use java -XshowSettings:properties to show the java.library.path (and others) value.

Jose Luis Martin
  • 10,459
  • 1
  • 37
  • 38
  • Thanks Jose, this helped me. I even found other important things with this `java.library.path` This path is lost after restarting the system, so I add it through /etc/profile file so that It automatically adds when the system restarts, even the `CLASSPATH` having related .jar files using these `java.library.path` .so files is overwritten by my application initiation's/catalina.sh's CLASSPATH, thus my application was complaining about the .jars and related .so's. Problem is fixed once `ClASSPATH` has the required .jar file and also the required .so file in the `java.library.path`. – Lihkinisak May 01 '13 at 17:10
  • 2
    For the confused runners of `java -XshowSettings:properties`. My version of java prints out the properties, and then prints out `--help`. So I need to scroll up to see the settings. – Tarrasch Nov 23 '15 at 09:38
  • Important note is to not have spaces for 'equal'-sign. Novice users may copy the command straight off and wonder why they get a "not a valid identifier" error. – Abdel Aleem Jun 25 '19 at 12:39
8

I had a lot of trouble figuring this out, please make sure that you have lib prefix in the library name.

So steps,

  1. export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/some/pathOfContainingDirectory"

  2. Rename libraries to have lib as a prefix. [Add this as part of build script]

    mv JNIDemo.so libJNIDemo.so
    

Check this answer for detailed explanation https://stackoverflow.com/a/3987567/2076566

muru
  • 4,723
  • 1
  • 34
  • 78
teardrop
  • 420
  • 6
  • 11
3

I used java -XshowSettings:properties method and found the path of a previously set folder and copied my so file to that folder

Ravindu
  • 2,408
  • 8
  • 30
  • 46
  • 1
    Hi @Ravindu I tried your approach. Its not working for me. I changed th permissions to 777 just to be sure. But nothing happens any suggestions? – Utsav Gupta Oct 07 '15 at 06:12
1

I believe this to be a better way to do it as it only needs one single link to be created, no file edits needed, no exports or running of source before an application is run. And no bashrc lines needed.

That said, if you upgrade it may cause unintentional errors to future java releases as it might load an outdated library, although one link removal also solves this.

This works under ubuntu 18.04 with java 11 opensdk installed.

$ locate libjawt.so
$ java -XshowSettings:properties

From the two commands above, you can find the two paths you need to adjust this to your own system.

sudo ln -s /path/from/locate/libjawt.so /path/from/properties/libawt.so

I made a symlink for java ll open sdk to find the library and mine was as follows because /usr/lib is one of the paths in the output. There is (in my output) a java path you may choose to use instead of /usr/lib if you want to keep java library (even symlinks) together.

sudo ln -s /usr/lib/jvm/java-11-openjdk-amd64/lib/libjawt.so /usr/lib/libjawt.so
CodingInTheUK
  • 930
  • 7
  • 16
0
File file = null;
private String[] libs_path = System.getProperty("java.library.path").split(":");
public boolean isInstalled() {
    boolean isInstalled = false;
    for(String lib : libs_path) {
            file = new File(lib+"/"+"yourlib.so");
            if(file.exists()) {
                isInstalled = true;
                break;
            }
        }
    return isInstalled;
}


public void install() {
    if(!isInstalled) {
        for(String lib: lib_path) {
             // copy your .so to lib
             break;
        }
    }
}