Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

native image fails with java.lang.ClassNotFoundException #2802

Closed
cmdjulian opened this issue Dec 5, 2022 · 9 comments · Fixed by #2849
Closed

native image fails with java.lang.ClassNotFoundException #2802

cmdjulian opened this issue Dec 5, 2022 · 9 comments · Fixed by #2849
Assignees
Labels
Milestone

Comments

@cmdjulian
Copy link

Describe the bug
When you create a Spring Boot 3 application with Spring Cloud Gateway from 2022.0.0-RC2 and include Spring Security and try to generate a native image it fails with:

Fatal error: java.lang.TypeNotPresentException: Type org.springframework.security.oauth2.client.ReactiveOAuth2AuthorizedClientManager not present
        at java.base/sun.reflect.generics.factory.CoreReflectionFactory.makeNamedType(CoreReflectionFactory.java:117)
        at java.base/sun.reflect.generics.visitor.Reifier.visitClassTypeSignature(Reifier.java:125)
        at java.base/sun.reflect.generics.tree.ClassTypeSignature.accept(ClassTypeSignature.java:49)
        at java.base/sun.reflect.generics.visitor.Reifier.reifyTypeArguments(Reifier.java:68)
        at java.base/sun.reflect.generics.visitor.Reifier.visitClassTypeSignature(Reifier.java:138)
------------------------------------------------------------------------------------------------------------------------
        at java.base/sun.reflect.generics.tree.ClassTypeSignature.accept(ClassTypeSignature.java:49)
        at java.base/sun.reflect.generics.repository.FieldRepository.computeGenericType(FieldRepository.java:90)
        at java.base/sun.reflect.generics.repository.FieldRepository.getGenericType(FieldRepository.java:82)
        at java.base/java.lang.reflect.Field.getGenericType(Field.java:276)
        at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.reflect.ReflectionDataBuilder.registerTypesForField(ReflectionDataBuilder.java:486)
        at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.reflect.ReflectionDataBuilder.processMethodMetadata(ReflectionDataBuilder.java:251)
        at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.reflect.ReflectionDataBuilder.duringAnalysis(ReflectionDataBuilder.java:199)
        at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.reflect.ReflectionFeature.duringAnalysis(ReflectionFeature.java:254)
        at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.NativeImageGenerator.lambda$runPointsToAnalysis$10(NativeImageGenerator.java:748)
        at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.FeatureHandler.forEachFeature(FeatureHandler.java:85)
        at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.NativeImageGenerator.lambda$runPointsToAnalysis$11(NativeImageGenerator.java:748)
        at org.graalvm.nativeimage.pointsto/com.oracle.graal.pointsto.AbstractAnalysisEngine.runAnalysis(AbstractAnalysisEngine.java:162)
        at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.NativeImageGenerator.runPointsToAnalysis(NativeImageGenerator.java:745)
        at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.NativeImageGenerator.doRun(NativeImageGenerator.java:578)
        at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.NativeImageGenerator.run(NativeImageGenerator.java:535)
        at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.NativeImageGeneratorRunner.buildImage(NativeImageGeneratorRunner.java:403)
        at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.NativeImageGeneratorRunner.build(NativeImageGeneratorRunner.java:580)
        at org.graalvm.nativeimage.builder/com.oracle.svm.hosted.NativeImageGeneratorRunner.main(NativeImageGeneratorRunner.java:128)
Caused by: java.lang.ClassNotFoundException: org.springframework.security.oauth2.client.ReactiveOAuth2AuthorizedClientManager
        at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:445)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:587)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
        at java.base/java.lang.Class.forName0(Native Method)
        at java.base/java.lang.Class.forName(Class.java:467)
        at java.base/sun.reflect.generics.factory.CoreReflectionFactory.makeNamedType(CoreReflectionFactory.java:114)
        ... 22 more

Sample
I created a Kotlin based sample application to show case the problem at https://github.com/cmdjulian/spring-cloud-gateway-bugs/tree/native-image-spring-security-bug. You can checkout the branch native-image-spring-security-bug. I used ./gradlew nativeCompile with liberica 22.3.r17-nik.

@cmdjulian
Copy link
Author

The issue still persists with the newest org.springframework.cloud:spring-cloud-dependencies:2022.0.0 release train

@spencergibb
Copy link
Member

Not really a gateway native issue, but spring security isn't it?

@OlgaMaciaszek
Copy link
Contributor

Yes, should probably be opened in Spring Security instead.

@cmdjulian
Copy link
Author

Are you guys able to move the issue or should I re-open it at Spring Security?

@spencergibb
Copy link
Member

We can't move it across GitHub orgs. You'll need to open a new issue. Thanks

@Azbesciak
Copy link

Azbesciak commented Jan 26, 2023

@spencergibb Since I have the same issue (new project created from initialz, only cloud gateway + security + graalvm), and guys from spring-security-ticket sent @cmdjulian back there, can you take a look? I do not need oauth, but it looks like required. I even explicit excluded every oauth lib from cloud's dependencies, did not help.

And my spring-security project for authorization does not fail during the native build, it also does not use oauth2.

I suppose the problem lies there: GatewayReactiveOAuth2AutoConfiguration but no idea why since it should be erased on the aot processing level

after autoconfiguration introspection I also excluded org.springframework.boot.autoconfigure.security.oauth2.resource.reactive.ReactiveOAuth2ResourceServerAutoConfiguration via spring.factories custom AutoConfigurationImportFilter, and also with (later, not together) with spring.autoconfigure.exclude; none of them worked.

...
I see that in the GatewayAutoConfiguration there is a fixed import to ReactiveOAuth2AuthorizedClientManager and OAuth2AuthorizedClient

...

the quick fix was to disable org.springframework.cloud.gateway.config.GatewayAutoConfiguration (excluded with spring.autoconfigure.exclude) and provide custom one without mentioning about any oauth. Worked.

...
But anyway I suppose you should take a look at HttpClientProperties it fails in the runtime (aot only) because of missing jakarta.el.ELManager, same as I see your @Bean + @ConfigurationProperties definition you may encounter spring-projects/spring-boot#33849 in spring boot 3.0.2

@Azbesciak
Copy link

@OlgaMaciaszek if you would build a native image, you may encounter also issues with @Validated annotated @ConfigurationProperties beans like GatewayProperties or HttpClientProperties.

Just a side note, right now it fails with that missing jakarta.el.ELManager, I suppose it is because of different @ConfigurationProperties processing in aot, but no time to investigate that

@OlgaMaciaszek
Copy link
Contributor

Thanks, @cmdjulian, @Azbesciak. Working on the hint registration issue. The @ConfigurationProperty issue seems unrelated and is probably something to be handled in Spring Boot.

@OlgaMaciaszek
Copy link
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
No open projects
Status: Done
5 participants