java.lang.UnsatisfiedLinkError even on setting -Djava.library.path

谁都会走 提交于 2019-12-24 17:19:00

问题


I am loading a library into my java code. I have put the library in the sytem 32 folder and I have also set the -Djava.library.path.

Earlier this code was running

try{


        System.loadLibrary("resources/TecJNI");

        System.out.println("JNI library loaded \n");
    }
    catch(UnsatisfiedLinkError e){
        System.out.println("Did not load library");
        e.printStackTrace();
    }

but since last week it is showing

java.lang.UnsatisfiedLinkError: no resources/TecJNI in java.library.path.

Is this some file permission issue for the dll that I am loading in the java code OR dll are using by some other application.

Also all other my running applications that were using & loading the same dll in different workspace are not running now.

Could anyone suggest me?

EDIT: I am using -

Djava.library.path="${workspace_loc}/org.syntec.ivb.application/resources;${env_var:PATH}"

in my eclipse vm arguments. I think it is using this.


回答1:


System.loadLibrary expects library name, not a path. The path to the directory containg the library should be set in PATH (Windows) env variable or in -Djava.library.path




回答2:


when comes to load libs in jvm, I like to copy the libs to a temp directory, then load them from the temp directory. here is the code:

private synchronized static void loadLib(String dllPath,String libName) throws IOException {
    String osArch = System.getProperty("os.arch").contains("64")?"_X64":"_X86";
    String systemType = System.getProperty("os.name");
    String libExtension = (systemType.toLowerCase().indexOf("win") != -1) ? ".dll"
            : ".so";
    String libFullName = libName+osArch+ libExtension;
    String nativeTempDir = System.getProperty("java.io.tmpdir");

    InputStream in = null;
    BufferedInputStream reader = null;
    FileOutputStream writer = null;

    File extractedLibFile = new File(nativeTempDir + File.separator
            + libFullName);
    if (!extractedLibFile.exists()) {
        try {
            in = new FileInputStream(dllPath+ File.separator+
                    libFullName);
            reader = new BufferedInputStream(in);
            writer = new FileOutputStream(extractedLibFile);

            byte[] buffer = new byte[1024];

            while (reader.read(buffer) > 0) {
                writer.write(buffer);
                buffer = new byte[1024];
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null)
                in.close();
            if (writer != null)
                writer.close();
        }
    }
    System.load(extractedLibFile.toString());
}



回答3:


Why do you need the additional "resources"?

When using System.loadLibrary("resources/TecJNI"); you are looking for TecJNI.dll in a subfolder "resources" of the java.library.path. So if you put C:\windows\system32 on the library-path (which you wouldn't need since it's on the search-path by default) your library should be C:\windows\system32\resources\TecJNI.dll



来源:https://stackoverflow.com/questions/18203075/java-lang-unsatisfiedlinkerror-even-on-setting-djava-library-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!