将 JNA 与 J9 一起使用的第一个困难是 J9 JVM 不包含 java.awt 包,并且 Native 类从该包中导入了一些类。这很容易通过下载 JNA 源代码、删除这些导入及其依赖方法(无论如何我都没有使用)并构建一个新的 JNA jar 来克服。
这是一个简单的测试程序:
public class TestJni {
public static void main(String[] args) {
CLibrary instance = (CLibrary) Native.loadLibrary((Platform.isWindows() ? "msvcrt" : "c"), CLibrary.class);
instance.printf("Hello, World\n", new Object[] {});
}
// This is the standard, stable way of mapping, which supports extensive
// customization and mapping of Java to native types.
public interface CLibrary extends Library {
void printf(String format, Object[] args);
}
}
更正 java.awt 问题后,我收到错误:
Caused by: java.lang.UnsatisfiedLinkError: C:\DOCUME~1\TSO0112\LOCALS~1\Temp\jna72681.dll (Incompatible JNI version (not 1.1, 1.2 or 1.4))
at java.lang.ClassLoader.loadLibraryWithPath(ClassLoader.java:973)
at java.lang.System.load(System.java:459)
at com.sun.jna.Native.loadNativeLibraryFromJar(Native.java:696)
at com.sun.jna.Native.loadNativeLibrary(Native.java:620)
at com.sun.jna.Native.<clinit>(Native.java:104)
at java.lang.J9VMInternals.initializeImpl(Native Method)
at java.lang.J9VMInternals.initialize(J9VMInternals.java:187)
at TestJni.main(TestJni.java:8)
“不兼容的 JNI 版本”是什么意思?有没有人让 J9 和 JNA 打得很好?
更新:我认为 JNA 在尝试加载 java.nio.Buffer 类时抑制了以下 NoClassDefFoundError ,因为 J9 显然不包含 NIO 包:
JNA: Problems loading core IDs: java.nio.Buffer
Exception in thread "main" java.lang.NoClassDefFoundError: java.nio.Buffer
at java.lang.ClassLoader.loadLibraryWithPath(Native Method)
at java.lang.ClassLoader.loadLibraryWithPath(ClassLoader.java:965)
at java.lang.System.load(System.java:459)
at TestJni.main(TestJni.java:8)