即使在设置-Djava.library.path时也存在java.lang.UnsatisfiedLinkError

时间:2013-08-13 07:28:06

标签: java dll java-native-interface unsatisfiedlinkerror

我正在将库加载到我的java代码中。 我已将库放在sytem 32文件夹中,并且还设置了-Djava.library.path。

此前此代码正在运行

try{


        System.loadLibrary("resources/TecJNI");

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

但自上周以来它正在显示

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

这是我在java代码中加载的dll的某些文件权限问题还是其他应用程序使用的dll。

所有其他正在运行的应用程序正在使用&在不同的工作空间中加载相同的dll现在没有运行。

有人可以建议我吗?

编辑:我正在使用 -

  

Djava.library.path = “$ {workspace_loc} /org.syntec.ivb.application/resources; $ {ENV_VAR:PATH}”

在我的eclipse vm参数中。我认为它正在使用它。

3 个答案:

答案 0 :(得分:2)

当在jvm中加载libs时,我喜欢将libs复制到临时目录,然后从temp目录加载它们。这是代码:

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());
}

答案 1 :(得分:1)

为什么需要额外的“资源”?

使用System.loadLibrary("resources/TecJNI");时,您正在java.library.path的子文件夹"resources"中查找TecJNI.dll。因此,如果您将C:\ windows \ system32放在库路径上(默认情况下它不在搜索路径上),您的库应为C:\windows\system32\resources\TecJNI.dll

答案 2 :(得分:1)

System.loadLibrary需要库名,而不是路径。应该在PATH(Windows)env变量或-Djava.library.path中设置包含库的目录的路径