[英]Can not use JNA: com.sun.jna.Library is not accessible
I am trying to use JNA, because I want to work with a.dll that was written in c++ and the rest of my code is in Java. I am trying to use JNA, because I want to work with a.dll that was written in c++ and the rest of my code is in Java. However, if I try to import the JNA classes Eclipse claims "The type com.sun.jna.Library is not accessible".但是,如果我尝试导入 JNA 类 Eclipse 声称“类型 com.sun.jna.Library 不可访问”。
My IDE seems to see that JNA is somehow in my libraries because when I type "import com.sun.j" it proposes.jna and the other packages in the.jar files to me.我的 IDE 似乎看到 JNA 在我的库中,因为当我键入“import com.sun.j”时,它会将.jna 和 .Z68995FCBF432492D40484D04A9D2 文件中的其他包
I have created a seperate Project to see if it works there, but it just doesnt work.我创建了一个单独的项目来查看它是否在那里工作,但它只是不起作用。
I have downloaded the latest.jar (jna-5.8.0.jar and jna-platform-5.8.0.jar) files from the jna Github (https://github.com/java-native-access/jna/blob/master/README.md ) and I have added them to the class path via project-> configure Buildpath -> add External JARs I have downloaded the latest.jar (jna-5.8.0.jar and jna-platform-5.8.0.jar) files from the jna Github (https://github.com/java-native-access/jna/blob/ master/README.md ),我已通过项目将它们添加到 class 路径->配置构建路径->添加外部 JARs
I am using Eclipse with javaSE-13.我将 Eclipse 与 javaSE-13 一起使用。
package Testtest;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Platform;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
Package strcuture: Package 结构:
Test
|-- src
| |-- TestTest
| | |-- Main.java
| |-- module-info.java
|-- JRE System Library [JavaSE-13]
|-- Referenced Libraries
|-- jna-5.8.0.jar
|-- jna-platform-5.8.0.jar
As @greg-449 mentioned in the comments, you have a modular project, indicated by the presence of the module-info.java
file in your src
directory (package root).正如评论中提到的@greg-449 ,您有一个模块化项目,由您的src
目录(包根目录)中存在module-info.java
文件表示。
You have three options here:您在这里有三个选择:
module-info.java
在忽略module-info.java
JRE 8 或更早版本上运行(或将您的项目兼容级别更改为)module-info.java
module descriptor in order to run your program without modules删除module-info.java
模块描述符,以便在没有模块的情况下运行程序module-info.java
) to add the directive requires com.sun.jna
.编辑您的模块描述符( module-info.java
)以添加指令requires com.sun.jna
。 If you're using jna-platform
then also add requires com.sun.jna.platform
.如果您使用的是 jna jna-platform
,则还requires com.sun.jna.platform
。
Structure
or PointerType
you will need to open
any packages in your project containing those subclasses to com.sun.jna
because they use reflection to access your subclasses.此外,如果您扩展任何 JNA 类(如Structure
或PointerType
),您将需要在项目中open
包含这些子类到com.sun.jna
的任何包,因为它们使用反射来访问您的子类。 (There are multiple options here, but open TestTest to com.sun.jna
is the closest to JDK8 behavior.). (这里有多个选项,但open TestTest to com.sun.jna
是最接近 JDK8 的行为。)。 I am aware this is just a test setup, but consider a reverse-DNS-style package name and module descriptor.我知道这只是一个测试设置,但考虑一个反向 DNS 样式的 package 名称和模块描述符。jna-jpms-5.8.0
and jna-platform-jpms-5.8.0
JARs as your dependencies, as they have their own module descriptors.如果您在模块路径上使用 JNA,您可能更喜欢使用jna-jpms-5.8.0
和jna-platform-jpms-5.8.0
JARs 作为依赖项,因为它们有自己的模块描述符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.