本文共 2831 字,大约阅读时间需要 9 分钟。
ClassNotFoundException是一个非运行时异常,且常常出现在项目启动时。它的意思简单明了,就是指项目启动时,或者执行某一个功能时,需要的Class找不到。这个错误非常常见,个人已经遇到过多次,但是每次的原因都不一定相同,
笔者这次遇到在使用springboot2.0的security相关配置时,启动时服务器报错,报错信息如下
Caused by: java.lang.NoClassDefFoundError: org/springframework/security/oauth2/common/util/RandomValueStringGenerator at org.cloudfoundry.identity.uaa.util.CachingPasswordEncoder.(CachingPasswordEncoder.java:71) at com.sap.ngom.datamigration.configuration.WebSecurityConfig.passwordEncoder(WebSecurityConfig.java:45) at com.sap.ngom.datamigration.configuration.WebSecurityConfig$$EnhancerBySpringCGLIB$$7b6105f3.CGLIB$passwordEncoder$2( ) at com.sap.ngom.datamigration.configuration.WebSecurityConfig$$EnhancerBySpringCGLIB$$7b6105f3$$FastClassBySpringCGLIB$$70546e9b.invoke( ) at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:361) at com.sap.ngom.datamigration.configuration.WebSecurityConfig$$EnhancerBySpringCGLIB$$7b6105f3.passwordEncoder( ) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ... 53 common frames omittedCaused by: java.lang.ClassNotFoundException: org.springframework.security.oauth2.common.util.RandomValueStringGenerator at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 65 common frames omitted
从最后打出的log来看,就是org.springframework.security.oauth2.common.util这个包下面的RandomValueStringGenerator类找不到,导致了这个错误,但是笔者在用oauth2相关配置之前,已经在maven里面引入了相关的依赖,如下:
org.springframework.cloud spring-cloud-starter-oauth2
且pom也没有报错,但是再次检查pom发现有下面这个现象:
因此判定解决这个unknown问题应该就能解决这个问题,准确的说还是依赖的问题导致需要的类找不到。最终通过修改pom中的依赖解决了这个问题。具体pom的修改这里不再展开。
pom配置文件中更改一下配置即可,
org.springframework.boot spring-boot-starter-tomcat provided
把scopt注释掉即可.
另一次遇到这个错误是在升级到spring boot2.1.5之后,解决办法:将dependency management里面的依赖去掉就没错误了。推测原因是升级会用到新的package,但是management里面指定的version没有这个class了。
参考
转载地址:http://pjpqi.baihongyu.com/