Tag Archives: java.lang.IllegalStateException: Failed to introspect Class

[Solved] java.lang.IllegalStateException: Failed to introspect Class xxxx

java.lang.IllegalStateException: Failed to introspect Class [com.central.es.config.RestAutoConfigure] from ClassLoader [sun.misc.Launcher$AppClassLoader@18b4aac2]
	at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:481)
	at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:358)
	at org.springframework.util.ReflectionUtils.getUniqueDeclaredMethods(ReflectionUtils.java:414)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.lambda$getTypeForFactoryMethod$2(AbstractAutowireCapableBeanFactory.java:742)
	at java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1660)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryMethod(AbstractAutowireCapableBeanFactory.java:741)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineTargetType(AbstractAutowireCapableBeanFactory.java:680)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:648)
	at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1614)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.java:523)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:495)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:620)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:612)
	at org.springframework.context.support.AbstractApplicationContext.getBeansOfType(AbstractApplicationContext.java:1243)
	at org.springframework.boot.SpringApplication.getExitCodeFromMappedException(SpringApplication.java:869)
	at org.springframework.boot.SpringApplication.getExitCodeFromException(SpringApplication.java:857)
	at org.springframework.boot.SpringApplication.handleExitCode(SpringApplication.java:844)
	at org.springframework.boot.SpringApplication.handleRunFailure(SpringApplication.java:795)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:325)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215)
	at com.central.MallCenterServer.main(MallCenterServer.java:29)
Caused by: java.lang.NoClassDefFoundError: org/elasticsearch/client/RestClientBuilder
	at java.lang.Class.getDeclaredMethods0(Native Method)
	at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
	at java.lang.Class.getDeclaredMethods(Class.java:1975)
	at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:463)
	... 21 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.elasticsearch.client.RestClientBuilder
	at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
	... 25 common frames omitted

The corresponding dependent scope in POM is test. Just change it to compile

Exception resolution: java.lang.IllegalStateException: Failed to introspect Class

Exception details

Exception encountered during context initialization – cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name xxxxx

Lookup method resolution failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [classA] from ClassLoader [sun.misc.Launcher$AppClassLoader@14dad5dc]

the reason

The reason for this exception is that a class that is not imported by the project is used in classA, which causes the class to fail to load. Generally speaking, if you use a class that has no dependencies, you should report a ClassNotFindException error, but if you only import but not use it, you may report this error.
Usually this error is related to the case where the scope of the third-party class configured using the maven framework is provided. The author caused this error when using spring boot and maven.

Solution

Check the non-dependent classes or packages that may exist in the classA class, import them (or add maven dependencies, or adjust the scope of dependencies in the dependent packages), in short, make the class loader can find this class in the current classPath.