Spring Boot项目正常运行,但使用maven打包后运行jar报错
PS E:\Program\IDEA\Projects\novel-springboot\novel-admin\target> java -jar .\main.jar
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.6.10)
2022-07-23 01:15:47.999 INFO 300 --- [ main] com.syt.admin.NovelAdminApplication : Starting NovelAdminApplication using Java 17.0.3.1 on DESKTOP-RJBV802 with PID 300 (E:\Program\IDEA\Projects\novel-springboot\novel-admin\target\main.jar started by xuxinyu in E:\Program\IDEA\Projects\novel-springboot\novel-admin\target)
2022-07-23 01:15:48.002 INFO 300 --- [ main] com.syt.admin.NovelAdminApplication : No active profile set, falling back to 1 default profile: "default"
2022-07-23 01:15:49.355 INFO 300 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8008 (http)
2022-07-23 01:15:49.363 INFO 300 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-07-23 01:15:49.364 INFO 300 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.65]
2022-07-23 01:15:49.422 INFO 300 --- [ main] .c.c.C.[.[.[/dev-api/vue-admin-template] : Initializing Spring embedded WebApplicationContext
2022-07-23 01:15:49.422 INFO 300 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1372 ms
Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter.
2022-07-23 01:15:49.543 INFO 300 --- [ main] c.a.d.s.b.a.DruidDataSourceAutoConfigure : Init DruidDataSource
2022-07-23 01:15:49.716 INFO 300 --- [ main] com.alibaba.druid.pool.DruidDataSource : {dataSource-1} inited
Registered plugin: 'com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor@6111ba37'
Parsed mapper file: 'URL [jar:file:/E:/Program/IDEA/Projects/novel-springboot/novel-admin/target/main.jar!/BOOT-INF/classes!/mapper/BookIndexMapper.xml]'
Parsed mapper file: 'URL [jar:file:/E:/Program/IDEA/Projects/novel-springboot/novel-admin/target/main.jar!/BOOT-INF/classes!/mapper/BookManageMapper.xml]'
Parsed mapper file: 'URL [jar:file:/E:/Program/IDEA/Projects/novel-springboot/novel-admin/target/main.jar!/BOOT-INF/classes!/mapper/SysPermissionMapper.xml]'
Parsed mapper file: 'URL [jar:file:/E:/Program/IDEA/Projects/novel-springboot/novel-admin/target/main.jar!/BOOT-INF/classes!/mapper/SysRoleMapper.xml]'
Parsed mapper file: 'URL [jar:file:/E:/Program/IDEA/Projects/novel-springboot/novel-admin/target/main.jar!/BOOT-INF/classes!/mapper/SysUserMapper.xml]'
Parsed mapper file: 'URL [jar:file:/E:/Program/IDEA/Projects/novel-springboot/novel-admin/target/main.jar!/BOOT-INF/lib/novel-common-0.0.1.jar!/mapper/AuthorMapper.xml]'
Parsed mapper file: 'URL [jar:file:/E:/Program/IDEA/Projects/novel-springboot/novel-admin/target/main.jar!/BOOT-INF/lib/novel-common-0.0.1.jar!/mapper/BookCategoryMapper.xml]'
Parsed mapper file: 'URL [jar:file:/E:/Program/IDEA/Projects/novel-springboot/novel-admin/target/main.jar!/BOOT-INF/lib/novel-common-0.0.1.jar!/mapper/BookContentMapper.xml]'
Parsed mapper file: 'URL [jar:file:/E:/Program/IDEA/Projects/novel-springboot/novel-admin/target/main.jar!/BOOT-INF/lib/novel-common-0.0.1.jar!/mapper/BookIndexMapper.xml]'
Parsed mapper file: 'URL [jar:file:/E:/Program/IDEA/Projects/novel-springboot/novel-admin/target/main.jar!/BOOT-INF/lib/novel-common-0.0.1.jar!/mapper/BookMapper.xml]'
_ _ |_ _ _|_. ___ _ | _
| | |\/|_)(_| | |_\ |_)||_|_\
/ |
3.5.1
2022-07-23 01:15:50.525 WARN 300 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customLoginFailureHandler': Lookup method resolution failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [com.syt.admin.security.CustomLoginFailureHandler] from ClassLoader [org.springframework.boot.loader.LaunchedURLClassLoader@3d71d552]
2022-07-23 01:15:50.526 INFO 300 --- [ main] com.alibaba.druid.pool.DruidDataSource : {dataSource-1} closing ...
2022-07-23 01:15:50.528 INFO 300 --- [ main] com.alibaba.druid.pool.DruidDataSource : {dataSource-1} closed
2022-07-23 01:15:50.532 INFO 300 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2022-07-23 01:15:50.555 INFO 300 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-07-23 01:15:50.589 ERROR 300 --- [ main] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customLoginFailureHandler': Lookup method resolution failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [com.syt.admin.security.CustomLoginFailureHandler] from ClassLoader [org.springframework.boot.loader.LaunchedURLClassLoader@3d71d552]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:289) ~[spring-beans-5.3.22.jar!/:5.3.22]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineConstructorsFromBeanPostProcessors(AbstractAutowireCapableBeanFactory.java:1302) ~[spring-beans-5.3.22.jar!/:5.3.22]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1219) ~[spring-beans-5.3.22.jar!/:5.3.22]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582) ~[spring-beans-5.3.22.jar!/:5.3.22]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.22.jar!/:5.3.22]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.22.jar!/:5.3.22]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.22.jar!/:5.3.22]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.22.jar!/:5.3.22]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.22.jar!/:5.3.22]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:955) ~[spring-beans-5.3.22.jar!/:5.3.22]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.22.jar!/:5.3.22]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.22.jar!/:5.3.22]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.6.10.jar!/:2.6.10]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:745) ~[spring-boot-2.6.10.jar!/:2.6.10]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:420) ~[spring-boot-2.6.10.jar!/:2.6.10]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) ~[spring-boot-2.6.10.jar!/:2.6.10]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1317) ~[spring-boot-2.6.10.jar!/:2.6.10]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) ~[spring-boot-2.6.10.jar!/:2.6.10]
at com.syt.admin.NovelAdminApplication.main(NovelAdminApplication.java:17) ~[classes!/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49) ~[main.jar:na]
at org.springframework.boot.loader.Launcher.launch(Launcher.java:108) ~[main.jar:na]
at org.springframework.boot.loader.Launcher.launch(Launcher.java:58) ~[main.jar:na]
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:88) ~[main.jar:na]
Caused by: java.lang.IllegalStateException: Failed to introspect Class [com.syt.admin.security.CustomLoginFailureHandler] from ClassLoader [org.springframework.boot.loader.LaunchedURLClassLoader@3d71d552]
at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:485) ~[spring-core-5.3.22.jar!/:5.3.22]
at org.springframework.util.ReflectionUtils.doWithLocalMethods(ReflectionUtils.java:321) ~[spring-core-5.3.22.jar!/:5.3.22]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:267) ~[spring-beans-5.3.22.jar!/:5.3.22]
... 26 common frames omitted
Caused by: java.lang.NoClassDefFoundError: org/springframework/boot/configurationprocessor/json/JSONException
at java.base/java.lang.Class.getDeclaredMethods0(Native Method) ~[na:na]
at java.base/java.lang.Class.privateGetDeclaredMethods(Class.java:3402) ~[na:na]
at java.base/java.lang.Class.getDeclaredMethods(Class.java:2504) ~[na:na]
at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:467) ~[spring-core-5.3.22.jar!/:5.3.22]
... 28 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.configurationprocessor.json.JSONException
at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:445) ~[na:na]
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:587) ~[na:na]
at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:151) ~[main.jar:na]
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520) ~[na:na]
... 32 common frames omitted
具体错误原因是org.springframework.boot.configurationprocessor.json.JSONException
这个类找不到了。经过一个多小时的排查发现此博客。
https://github.com/spring-projects/spring-boot/issues/24244
https://github.com/spring-projects/spring-boot/issues/22036
原因是项目pom使用了这个依赖,并且代码中使用了此依赖的类
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.boot.configurationprocessor.json.JSONObject;
注意它是带有<optional>true</optional>
可选标记的,编译打包时不会打包进去,导致打包后启动报错
解决方案
修改代码中的引用,JSON使用fastJason而不是spring-boot-configuration-processor中的JSON
评论区