Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. However, if I try to import the JNA classes Eclipse claims "The type com.sun.jna.Library is not accessible".

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.

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 am using Eclipse with 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:

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
like image 447
vicki-tesch Avatar asked Oct 14 '22 21:10

vicki-tesch


1 Answers

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).

You have three options here:

  • run on (or change your project compatibility level to) JRE 8 or earlier which ignores module-info.java
  • delete the module-info.java module descriptor in order to run your program without modules
  • edit your module descriptor (module-info.java) to add the directive requires com.sun.jna. If you're using jna-platform then also add 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. (There are multiple options here, but open TestTest to com.sun.jna is the closest to JDK8 behavior.). I am aware this is just a test setup, but consider a reverse-DNS-style package name and module descriptor.
    • 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.
like image 177
Daniel Widdis Avatar answered Oct 18 '22 14:10

Daniel Widdis