简体   繁体   English

无法使用 JNA:com.sun.jna.Library is not access

[英]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:您在这里有三个选择:

  • run on (or change your project compatibility level to) JRE 8 or earlier which ignores module-info.java在忽略module-info.java JRE 8 或更早版本上运行(或将您的项目兼容级别更改为)
  • delete the module-info.java module descriptor in order to run your program without modules删除module-info.java模块描述符,以便在没有模块的情况下运行程序
  • edit your module descriptor ( 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
    • In addition, if you extend any JNA classes like 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 类(如StructurePointerType ),您将需要在项目中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 名称和模块描述符。
    • If you are using JNA on the module path you may prefer to use the 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.0jna-platform-jpms-5.8.0 JARs 作为依赖项,因为它们有自己的模块描述符。

暂无
暂无

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

相关问题 com.sun.jna.Pointer无法转换为com.sun.jna.platform.win32.WinDef.LPARAM - com.sun.jna.Pointer cannot be cast to com.sun.jna.platform.win32.WinDef.LPARAM Ant 错误“线程异常”AWT-EventQueue-0“java.lang.NoClassDefFoundError:com/sun/jna/Library” - Ant error “Exception in thread ”AWT-EventQueue-0“ java.lang.NoClassDefFoundError: com/sun/jna/Library” 在资源路径中找不到本机库(com / sun / jna / android-aarch64 / libjnidispatch.so) - Native library (com/sun/jna/android-aarch64/libjnidispatch.so) not found in resource path 我可以通过JNA从Java使用COM吗? - Can I use COM from Java via JNA? 无法从类com.sun.jna.Native的Native(Method,Object)获取静态方法 - Can't obtain static method fromNative(Method, Object) from class com.sun.jna.Native java.lang.UnsatisfiedLinkError:无法获取com.sun.jna.Pointer类(神经技术Megamatcher SDK) - java.lang.UnsatisfiedLinkError: Can't obtain class com.sun.jna.Pointer (Neurotechnology Megamatcher SDK) 未找到 JNA。 本机方法将被禁用。 java.lang.ClassNotFoundException: com.sun.jna.Native - JNA not found. native methods will be disabled. java.lang.ClassNotFoundException: com.sun.jna.Native 如何使用JNA将com.sun.jna.Structure从Java传递到C中的struct - How to pass a com.sun.jna.Structure from Java to struct in C, using JNA Cassandra 无法初始化 class com.sun.jna.Native - Cassandra Could not initialize class com.sun.jna.Native JNA:com.sun.jna.platform.win32.Win32Exception-访问被拒绝 - JNA:com.sun.jna.platform.win32.Win32Exception- access denied
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM