JNA Native initialization issue in IDEA 2022.3

已回答
Asked from a ticket so forwarding here.

In 2022.3 RC Build #IC-223.7571.123, create a simple Java project as below, set the IDEA as SDK (Intellij Platform Plugin SDK) and run.


```
import com.sun.jna.Library;
import com.sun.jna.Native;
public class Main {


public class Kernel32 implements Library {
Kernel32 INSTANCE = Native.load("kernel32", Kernel32.class);
}
public static void main(String[] args) {
Native.load("kernel32", Kernel32.class);
System.out.println("Hello world!");
}
}
```
We will get
```
C:\Users\xxxxxxx\work\tools\jdk-17.0.1\bin\java.exe -javaagent:C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\idea_rt.jar=59250:C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\bin -Dfile.encoding=UTF-8 -classpath C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\3rd-party-native.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\3rd-party-rt.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\annotations.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\app.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\byte-buddy-agent.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\error-prone-annotations.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\external-system-rt.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\externalProcess-rt.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\forms_rt.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\groovy.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\idea_rt.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\intellij-coverage-agent-1.0.682.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\intellij-test-discovery.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\jps-model.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\jsch-agent.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\jsp-base.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\junit4.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\kotlin-script-runtime.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\platform-duplicates-analysis.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\platform-objectSerializer-annotations.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\platform-statistics-devkit.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\platform-structuralSearch.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\protobuf.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\rd.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\stats.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\testFramework.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\tools-testsBootstrap.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\util.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\util_rt.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\xml-dom-impl.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\xml-dom.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\plugins\java\lib\aether-dependency-resolver.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\plugins\java\lib\debugger-memory-agent.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\plugins\java\lib\java-impl.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\plugins\java\lib\javac2.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\plugins\java\lib\jb-jdi.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\plugins\java\lib\jgoodies-common.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\plugins\java\lib\jps-builders-6.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\plugins\java\lib\jps-builders.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\plugins\java\lib\jps-javac-extension.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\plugins\java\lib\jps-launcher.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\plugins\java\lib\jshell-frontend.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\plugins\java\lib\jshell-protocol.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\plugins\java\lib\sa-jdwp.jar;C:\Users\xxxxxxx\IdeaProjects\memo\out\production\memo Main
Exception in thread "main" java.lang.UnsatisfiedLinkError: Native library (com/sun/jna/win32-x86-64/jnidispatch.dll) not found in resource path (C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\3rd-party-native.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\3rd-party-rt.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\annotations.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\app.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\byte-buddy-agent.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\error-prone-annotations.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\external-system-rt.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\externalProcess-rt.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\forms_rt.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\groovy.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\idea_rt.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\intellij-coverage-agent-1.0.682.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\intellij-test-discovery.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\jps-model.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\jsch-agent.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\jsp-base.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\junit4.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\kotlin-script-runtime.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\platform-duplicates-analysis.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\platform-objectSerializer-annotations.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\platform-statistics-devkit.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\platform-structuralSearch.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\protobuf.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\rd.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\stats.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\testFramework.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\tools-testsBootstrap.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\util.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\util_rt.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\xml-dom-impl.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\lib\xml-dom.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\plugins\java\lib\aether-dependency-resolver.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\plugins\java\lib\debugger-memory-agent.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\plugins\java\lib\java-impl.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\plugins\java\lib\javac2.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\plugins\java\lib\jb-jdi.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\plugins\java\lib\jgoodies-common.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\plugins\java\lib\jps-builders-6.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\plugins\java\lib\jps-builders.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\plugins\java\lib\jps-javac-extension.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\plugins\java\lib\jps-launcher.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\plugins\java\lib\jshell-frontend.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\plugins\java\lib\jshell-protocol.jar;C:\Users\xxxxxxx\work\tools\ideaIC-223.7571.123.win\plugins\java\lib\sa-jdwp.jar;C:\Users\xxxxxxx\IdeaProjects\memo\out\production\memo)
at com.sun.jna.Native.loadNativeDispatchLibraryFromClasspath(Native.java:1059)
at com.sun.jna.Native.loadNativeDispatchLibrary(Native.java:1015)
at com.sun.jna.Native.<clinit>(Native.java:221)
at Main.main(Main.java:10)


Process finished with exit code 1
```


Actually the JNA could initialize successfully in IDEAC-2022.2.3
I compare the 2022.2.3 with 2022.3 RC.
A JNA.jar is provided in 2022.2.3, which provide class `com.sun.jna.Native` and `com.sun.jna/win32-x86-64/jndispatch.dll`, but in 2022.3 RC, a 3rd-party-rt.jar is provided which only contains `com.sun.jna.Native` and `jndispatch.dll` is missing.

It seems the newer version put the `jndispatch.dll` in <IDE_installation_folder>/lib/jna/ directly instead of using `lib/jna.jar` in the IDEA 2022.3 because of this issue https://youtrack.jetbrains.com/issue/IDEA-256256/
I am tuning my IDEA plugin to the latest version of IDEA, actually my IDEA plugin already contains a JNA.jar, but it seems still using the 3rd-party-rt.jar in 2022.3 RC, is there any way to add configuration to ask the IDEA to load my JNA.jar firstly during startup?

Any way to let the IDEA load my JNA.jar first or any other way to fix this JNA issue?
There is a guide here about using the Gradle to let the plugin use its library first: https://plugins.jetbrains.com/docs/intellij/plugin-class-loaders.html#overriding-ide-dependencies
But my plugin is based on maven, the guide is about Gradle. Do you have more details about the setting in Plugin DevKit?
Thanks.
0

`System.setProperty("jna.nounpack", "false");` should work.

0

@Zhengyil Thanks a lot! From my checking, that line is not enough but with the below one, it works now:

System.setProperty("jna.nounpack", "false");
System.setProperty("jna.boot.library.path", PathManager.getHomePath() + "/lib/jna/" + System.getProperty("os.arch"));
0

I encountered the same problem in version 2022.2 and 2022.3 and solved the problem using the method you provided. However, in version 2022.3.1, there is a new error: Unable to locate the JNA native support library.
My plug-in is very simple, relying only on oshi, implementation("com.github.oshi:oshi-core:6.6.0").

0

请先登录再写评论。