Search code examples
javaamazon-web-servicesmicronautgraalvm

GraalVM native image with micronaut and aws sdk-v2


I have an issue when building a GraalVM native image using micronaut and aws sdk-v2. Before detailing the error I want to note that before using the sdk-v2 the native image was built correctly.

Once I updated to aws sdk-v2 (via micronaut dependency as):

...
implementation "io.micronaut.aws:micronaut-aws-sdk-v2:2.0.2"
...

I get an error when the trying to built the image

Fatal error:java.lang.NoSuchMethodError
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
    at java.base/java.util.concurrent.ForkJoinTask.getThrowableException(ForkJoinTask.java:603)
    at java.base/java.util.concurrent.ForkJoinTask.get(ForkJoinTask.java:1006)
    at com.oracle.svm.hosted.NativeImageGenerator.run(NativeImageGenerator.java:480)
    at com.oracle.svm.hosted.NativeImageGeneratorRunner.buildImage(NativeImageGeneratorRunner.java:349)
    at com.oracle.svm.hosted.NativeImageGeneratorRunner.build(NativeImageGeneratorRunner.java:508)
    at com.oracle.svm.hosted.NativeImageGeneratorRunner.main(NativeImageGeneratorRunner.java:114)
    at com.oracle.svm.hosted.NativeImageGeneratorRunner$JDK9Plus.main(NativeImageGeneratorRunner.java:537)
Caused by: java.lang.NoSuchMethodError: 'void io.micronaut.core.graal.AutomaticFeatureUtils.initializeAtBuildTime(org.graalvm.nativeimage.hosted.Feature$BeforeAnalysisAccess, java.lang.String)'
    at io.micronaut.aws.sdk.v2.graal.AwsSdkAutomaticFeature.beforeAnalysis(AwsSdkAutomaticFeature.java:41)
    at com.oracle.svm.hosted.NativeImageGenerator.lambda$runPointsToAnalysis$7(NativeImageGenerator.java:693)
    at com.oracle.svm.hosted.FeatureHandler.forEachFeature(FeatureHandler.java:70)
    at com.oracle.svm.hosted.NativeImageGenerator.runPointsToAnalysis(NativeImageGenerator.java:693)
    at com.oracle.svm.hosted.NativeImageGenerator.doRun(NativeImageGenerator.java:555)
    at com.oracle.svm.hosted.NativeImageGenerator.lambda$run$0(NativeImageGenerator.java:468)
    at java.base/java.util.concurrent.ForkJoinTask$AdaptedRunnableAction.exec(ForkJoinTask.java:1407)
    at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:290)
    at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1020)
    at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1656)
    at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1594)
    at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:183)
Error: Image build request failed with exit status 1
com.oracle.svm.driver.NativeImage$NativeImageError: Image build request failed with exit status 1
    at com.oracle.svm.driver.NativeImage.showError(NativeImage.java:1558)
    at com.oracle.svm.driver.NativeImage.build(NativeImage.java:1308)
    at com.oracle.svm.driver.NativeImage.performBuild(NativeImage.java:1269)
    at com.oracle.svm.driver.NativeImage.main(NativeImage.java:1228)
    at com.oracle.svm.driver.NativeImage$JDK9Plus.main(NativeImage.java:1740)
...

My assumption was that micronaut must be doing some reflection internally and that is making the build process to fail. I have added the following json file to set a reflection configuartion

[
  {
    "name" : "io.micronaut.core.graal.AutomaticFeatureUtils",
    "allDeclaredConstructors" : true,
    "allPublicConstructors" : true,
    "allDeclaredMethods" : true,
    "allPublicMethods" : true
  },
  {
    "name":"io.micronaut.core.graal.AutomaticFeatureUtils",
    "methods":[{"name":"initializeAtBuildTime","parameterTypes":["org.graalvm.nativeimage.hosted.Feature$BeforeAnalysisAccess","java.lang.String[]"] }]
  }
]

However the error persists. Not sure if it is due to the fact that AutomaticFeatureUtils.initializeAtBuildTime is expecting a varargs of strings and however in the stacktrace I can see just one String being passed, or the error is not related to reflection at all.

No matter what I try, that method is not found when building the image.

Any idea on what I may be missing here?

Thanks!!


Solution

  • This error is not related to reflection.

    Feature and AutomaticFeature are GraalVM native image concepts that allow programmatic configuration for the native iamge, more details can be found for example in the javadoc: https://github.com/oracle/graal/blob/master/sdk/src/org.graalvm.nativeimage/src/org/graalvm/nativeimage/hosted/Feature.java#L59

    The error you show happens during the image generation, this is a normal java process, which doesn't need to have reflection configured etc. Those are the configuration for the runtime behavior.

    What the error says is that AwsSdkAutomaticFeature (source tries to call void io.micronaut.core.graal.AutomaticFeatureUtils.initializeAtBuildTime(org.graalvm.nativeimage.hosted.Feature$BeforeAnalysisAccess, java.lang.String) but there's no such method. Most probable cause is the version mismatch between the dependency and the core micronaut library that contain graal support.