1

我正在将一个库加载到我的 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 参数中。我认为它正在使用这个。

4

3 回答 3

2

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

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());
}
于 2013-08-13T08:50:07.753 回答
1

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

使用时,System.loadLibrary("resources/TecJNI");您在 java.library.path 的子文件夹中寻找 TecJNI.dll "resources"。所以如果你把 C:\windows\system32 放在库路径上(你不需要,因为它默认在搜索路径上)你的库应该是C:\windows\system32\resources\TecJNI.dll

于 2013-08-13T07:49:49.227 回答
1

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

于 2013-08-13T08:11:58.840 回答