English 中文(简体)
How to with with
原标题:How to register method for runtime reflection with GraalVM?

I am trying to get an existing Java application running with GraalVM. Now I have encountered an issue i don t know how to solve. I was able to successfully create a native image. However I had to create a reflection configuration like this to overcome the "Warning: Reflection method java.lang.Class.getMethod invoked" message during the compile phase:

[
  {
    "name": "java.lang.Class",
    "queryAllDeclaredConstructors": true,
    "queryAllPublicConstructors": true,
    "queryAllDeclaredMethods": true,
    "queryAllPublicMethods": true,
    "allDeclaredClasses": true,
    "allPublicClasses": true
  },
  {
    "name": "org.apache.logging.log4j.message.DefaultFlowMessageFactory",
    "queryAllDeclaredConstructors": true,
    "queryAllPublicConstructors": true,
    "queryAllDeclaredMethods": true,
    "queryAllPublicMethods": true,
    "allDeclaredClasses": true,
    "allPublicClasses": true
  }
]

我增加了第二个条目,因为我的本土形象正在扔下一种没有产生方法的错误,用于org.apache.logging.log4j.message.DefaultFlowMessageFactory.<init>

当我现在掌握我的本土形象时,我有以下错误:

Exception in thread "main" org.graalvm.nativeimage.MissingReflectionRegistrationError: The program tried to reflectively invoke method public org.apache.logging.log4j.message.DefaultFlowMessageFactory() without it being registered for runtime reflection. Add it to the reflection metadata to solve this problem. See https://www.graalvm.org/latest/reference-manual/native-image/metadata/#reflection for help.
    at org.graalvm.nativeimage.builder/com.oracle.svm.core.reflect.MissingReflectionRegistrationUtils.forQueriedOnlyExecutable(MissingReflectionRegistrationUtils.java:97)
    at java.base@17.0.8/java.lang.reflect.Constructor.acquireConstructorAccessor(Constructor.java:74)
    at java.base@17.0.8/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:496)
    at java.base@17.0.8/java.lang.reflect.ReflectAccess.newInstance(ReflectAccess.java:128)
    at java.base@17.0.8/jdk.internal.reflect.ReflectionFactory.newInstance(ReflectionFactory.java:347)
    at java.base@17.0.8/java.lang.Class.newInstance(DynamicHub.java:645)
    at org.apache.logging.log4j.spi.AbstractLogger.createDefaultFlowMessageFactory(AbstractLogger.java:240)
    at org.apache.logging.log4j.spi.AbstractLogger.<init>(AbstractLogger.java:141)
    at org.apache.logging.log4j.status.StatusLogger.<init>(StatusLogger.java:141)
    at org.apache.logging.log4j.status.StatusLogger.<clinit>(StatusLogger.java:91)
    at org.apache.logging.slf4j.Log4jMarkerFactory.<clinit>(Log4jMarkerFactory.java:36)
    at org.apache.logging.slf4j.SLF4JServiceProvider.initialize(SLF4JServiceProvider.java:53)
    at org.slf4j.LoggerFactory.bind(LoggerFactory.java:183)
    at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:170)
    at org.slf4j.LoggerFactory.getProvider(LoggerFactory.java:455)
    at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:441)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:390)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:416)
    at app.Main.<clinit>(Main.java:12)

错误信息意味着,我也许能够解决这个问题,登记这一方法进行实时思考。

OK,但如何? 我找不到对文件有任何帮助。

任何想法? 是否有任何例子我可能错失?

问题回答

我今天面临一个类似的问题(春季间3.1.2)。

  1. mvn -Pnative native:compile
  2. java -Dspring.aot.enabled=true -agentlib:native-image-agent=config-output-dir=./config -jar target/<your-app.jar> (this will create a config folder in the root dir of your app)
  3. try testing your application with all the possible paths covered so that reflection-config.json has all the information regarding the reflection calls at run time, once done use Ctrl+C to stop the app
  4. create a folder META-INF/native-image in src/main/resources
  5. copy all the contents here(META-INF/native-image) from config folder created in step 2 enter image description here
  6. again run mvn -Pnative native:compile OR mvn -Pnative spring-boot:build-image(if you need docker image)
  7. finally run target/your-app-name(or docker run...)

然而,我希望,在试图学习的草原和泉水池方面,我是一个新事物。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签