1

我的应用程序的 Graalvm 本机映像在以下方法调用中运行时失败:

Unsafe.allocateInstance(org.lwjgl.PointerBuffer.class);

有这个特例:

java.lang.IllegalArgumentException: Class org.lwjgl.PointerBuffer is instantiated reflectively but was never registered. Register the class by using org.graalvm.nativeimage.hosted.RuntimeReflection
    at com.oracle.svm.core.graal.snippets.SubstrateAllocationSnippets.hubErrorStub(SubstrateAllocationSnippets.java:246)

在生成本机图像之前,我启动了应用程序,native-image-agent它生成了 graalvm 配置 jsons,但reflect-config.json没有org.lwjgl.PointerBuffer.

即使我手动添加到reflect-config.json

{
  "name":"org.lwjgl.PointerBuffer",
  "allDeclaredFields":true,
  "allPublicConstructors" : true
  "allPublicFields": true,
  "allDeclaredMethods": true,
  "allPublicMethods": true
}

由于上述异常,本机映像仍然失败。有没有办法Unsafe.allocateInstance在原生图像中工作?

4

1 回答 1

0

我不确定它是否对你有帮助。该类与“CustomBuffer”非常相似。尝试将此配置添加到您的反射.json 并测试它是否有效。

{
  "name" : "org.lwjgl.PointerBuffer",
  "fields":[
    {"name":"capacity", "allowUnsafeAccess":true}, 
    {"name":"container", "allowUnsafeAccess":true}, 
    {"name":"limit", "allowUnsafeAccess":true}, 
    {"name":"mark", "allowUnsafeAccess":true}, 
    {"name":"position", "allowUnsafeAccess":true}
  ]
}

我在我的代码中测试它并且它有效。我希望它对你有帮助。

于 2020-07-05T17:19:25.260 回答