简体   繁体   English

找不到使用JNA调用的DLL所需的文件

[英]Cannot Find The File needed by the DLL that is Called Using JNA

I have a dll (let's name it Sample.dll) that is being called by a Java application thru JNA. 我有一个Java应用程序通过JNA调用的dll(将其命名为Sample.dll)。 The said dll locates a file inside a folder named "Data". 所述dll在名为“ Data”的文件夹内找到文件。 I think the said dll was loaded successfully since there was no error message being returned. 我认为该dll已成功加载,因为没有错误消息被返回。 Below is the code to load the dll: 以下是加载dll的代码:

sampleLibrary = (SampleLibrary)Native.loadLibrary("Sample", SampleLibrary.class);

After execution of that code, a native method is called to open a session. 执行该代码后,将调用本机方法来打开会话。 This method accepts the path of a folder as a parameter. 此方法接受文件夹的路径作为参数。

sampleLibrary.openSession(path);

The JNA cannot seem to locate the file since the error is being returned that says the file does not exist. JNA似乎无法找到该文件,因为返回了错误消息,指出该文件不存在。 I try to set and print the "user.dir" and the "java.library.path" to see if the path being passed is correct. 我尝试设置并打印“ user.dir”和“ java.library.path”,以查看所传递的路径是否正确。

System.setProperty("java.library.path", "C:/Sample");
System.out.println("user.dir property: " + System.getProperty("user.dir"));
        System.out.println("java.library.path property: " + System.getProperty("java.library.path"));

These return the working directory where the jar, dll and a Data folder are located. 这些返回jar,dll和Data文件夹所在的工作目录。

File Structure: 档案结构:

C:\Sample
  \-- SampleJna.jar
  \-- Sample.dll
  \-- Data 
      \----- some files

First, the .loadLibrary is being deprecated. 首先,不赞成使用.loadLibrary Try the Native.load() instead. 请尝试使用Native.load() Sample below as stated here : 下面的示例为说明这里

SampleLibrary INSTANCE = (SampleLibrary) Native.load((Platform.isWindows() ? "Sample" : "c"), SampleLibrary.class);

Second, in your project properties under the VM Options (if you are using Netbeans), make sure the library path is declared like so: -Djna.library.path=C:\\Sample\\ 其次,在“ VM选项”下的项目属性中(如果您使用的是Netbeans),请确保库路径的声明如下: -Djna.library.path=C:\\Sample\\

If you still get some error, please post your stacktrace so everyone can check. 如果仍然出现错误,请发布您的stacktrace,以便每个人都可以检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM