Using 'ImportAutoConfigurationImportSelector' in the jar package loaded by the custom class loader will raise `ClassNotFoundException`.
See original GitHub issueI try to use ‘UrlClassLoader’ to load a jar package. In this jar package, an ‘AnnotationConfigApplicationContext’ will be started, and the ‘@ImportAutoConfiguration’ annotation will be used. At this time, my application will throw an ‘IllegalargumentException’ caused by ‘ClassNotFoundException’.
I have a simple example. (https://github.com/aoyvx/CustomClassLoaderExample). The following stack will be thrown when the module’app-one 'is run.
Exception in thread "main" java.lang.IllegalArgumentException: Could not find class [org.example.impl.SimpleConfig]
at org.springframework.util.ClassUtils.resolveClassName(ClassUtils.java:334)
at org.springframework.boot.autoconfigure.ImportAutoConfigurationImportSelector.getAnnotations(ImportAutoConfigurationImportSelector.java:132)
at org.springframework.boot.autoconfigure.ImportAutoConfigurationImportSelector.getCandidateConfigurations(ImportAutoConfigurationImportSelector.java:77)
at org.springframework.boot.autoconfigure.AutoConfigurationImportSelector.getAutoConfigurationEntry(AutoConfigurationImportSelector.java:125)
at org.springframework.boot.autoconfigure.AutoConfigurationImportSelector$AutoConfigurationGroup.process(AutoConfigurationImportSelector.java:440)
at org.springframework.context.annotation.ConfigurationClassParser$DeferredImportSelectorGrouping.getImports(ConfigurationClassParser.java:879)
at org.springframework.context.annotation.ConfigurationClassParser$DeferredImportSelectorGroupingHandler.processGroupImports(ConfigurationClassParser.java:809)
at org.springframework.context.annotation.ConfigurationClassParser$DeferredImportSelectorHandler.process(ConfigurationClassParser.java:780)
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:193)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:331)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:247)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:311)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:112)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:746)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:564)
at org.example.impl.EchoApp.init(EchoApp.java:15)
at com.example.app.App.main(App.java:15)
Caused by: java.lang.ClassNotFoundException: org.example.impl.SimpleConfig
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
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 org.springframework.util.ClassUtils.forName(ClassUtils.java:284)
at org.springframework.util.ClassUtils.resolveClassName(ClassUtils.java:324)
... 16 more
Disconnected from the target VM, address: '127.0.0.1:54192', transport: 'socket'
Process finished with exit code 1
I modified ImportAutoConfigurationImportSelector#getExclusions and ImportAutoConfigurationImportSelector#getAnnotations in module app-b, replace null with getBeanClassLoader(). at this time, the program can run normally.
Issue Analytics
- State:
- Created a year ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
Using 'ImportAutoConfigurationImportSelector' in the ... - GitHub
Using 'ImportAutoConfigurationImportSelector' in the jar package loaded by a custom class loader throws ClassNotFoundException #31801.
Read more >How to Fix ClassNotFoundException in Java - Rollbar
The Java ClassNotFoundException occurs when the JVM tries to load a class ... in methods which can throw this exception - either by...
Read more >java - ClassNotFoundException while trying to load class from ...
The main challenge here is to find part of byte array that contains your class - since you have whole jar as a...
Read more >Java ClassLoader | DigitalOcean
It loads rt.jar and other core classes for example java.lang. ... Any class in the com.journaldev * package will be loaded using this ......
Read more >Class Loaders in Java - Baeldung
Have a look at the behind-the-scenes of loading Java classes during ... how we can load different classes using various class loaders:
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I have a recollection of doing a sweep of the codebase trying to consistently use the bean class loader or the class loader from the resource loader or something like that. I can’t find the issue either though 🤷.
+1, particularly as we already use the bean class loader to load the factory names. We should consistently use the same class loader.
Yes please, @aoyvx. A PR would be most welcome.
Closing in favor of PR #31798