How to add .so file to the java.library.path in Li

2019-01-21 16:08发布

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)

4条回答
迷人小祖宗
2楼-- · 2019-01-21 16:44

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

查看更多
Viruses.
3楼-- · 2019-01-21 16:48

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

查看更多
淡お忘
4楼-- · 2019-01-21 17:04
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;
        }
    }
}
查看更多
叼着烟拽天下
5楼-- · 2019-01-21 17:06

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.

查看更多
登录 后发表回答