广东省住房城乡建设厅网站,爱站网工具包,想学广告设计没有基础,软件外包平台Maven项目的Jar包打包问题-没有主清单属性ClassNotFoundException 与 NoClassDefFoundError 文章目录 Maven项目的Jar包打包问题-没有主清单属性ClassNotFoundException 与 NoClassDefFoundError1、问题出现1.1、Jar包运行#xff1a;没有主清单属性解决方…Maven项目的Jar包打包问题-没有主清单属性ClassNotFoundException 与 NoClassDefFoundError 文章目录 Maven项目的Jar包打包问题-没有主清单属性ClassNotFoundException 与 NoClassDefFoundError1、问题出现1.1、Jar包运行没有主清单属性解决方案1.2、Springboot打包 Jar 出现 java.lang.NoClassDefFoundError 的问题解决方案 2、参考文献 这两个问题的出现场景是你打包完一个SpringBoot、Maven项目上传Jar包到服务器运行的时候遇到的。也算是比较经典的两个问题了如果你在打包项目的时候很容易遇到这篇文章就是用来一劳永逸地解决它们。 1、问题出现
1.1、Jar包运行没有主清单属性 解决方案
其实这个问题主要是在IDEA打包环节出现了问题当我们对打包好的jar包进行解压以后会发现有一个MANIFEST.MF文件此文件就是jar运行时要查找的清单目录。 主清单数据就是我们要运行的主类即程序入口缺少主清单属性就不知道从哪开始运行。 因此我们需要对项目进行配置指定程序入口。
我们需要在POM文件中引入
plugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactId
/pluginspring-boot-maven-plugin是一个Spring Boot插件用于简化Spring Boot项目的构建和打包。它可以自动化地创建可执行的JAR文件包括一个嵌入式的Tomcat服务器这样你就可以直接运行你的应用程序而不需要先安装Java或Tomcat。
1.2、Springboot打包 Jar 出现 java.lang.NoClassDefFoundError 的问题 Caused by: java.lang.ClassNotFoundException: AAA Caused by: java.lang.NoClassDefFoundError: AAA Caused by: java.lang.reflect.InvocationTargetException java.lang.IllegalArgumentException: Unable to create serializer “com.esotericsoftware.kryo.serializers.FieldSerializer” for class:com.xxx.yyy.BBB 这个问题的出现其实还是跟打包插件有关系我不赞同这篇文章的说法 《【Springboot】打包 Jar 出现 java.lang.NoClassDefFoundError 的问题》
这篇文章说和引入依赖本身有问题。大家可以试试这个文章的解决方案。
解决方案
谈到maven的打包我们需要知道我们平时打包的插件有两种
maven-jar-plugin plugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-compiler-plugin/artifactId/pluginmaven-assembly-plugin[推荐]
这个插件的主要作用是帮助你构建一个包含所有依赖的JAR文件。 pluginartifactIdmaven-assembly-plugin/artifactIdconfigurationappendAssemblyIdtrue/appendAssemblyIddescriptorRefsdescriptorRefjar-with-dependencies/descriptorRef/descriptorRefs/configuration/plugin前者为原始jar包类似于maven-jar打包里面的origin-jar后者with-dependencies为包含pom中费provided依赖的jar包如果线上环境未提供这些依赖就得使用with-dependencies的jar包。
解决 使用 maven-assembly-plugin 上传了原始jar包而生产环境中没有AAA所以BBB中调用显示no class found改为上传with-dependicies包后程序正常运行.
我在打包好jar包以后上传了服务器在服务器运行jar包的时候同时遇到了上面两个问题最后我的pom文件是这么写的 buildpluginspluginartifactIdmaven-assembly-plugin/artifactIdconfigurationappendAssemblyIdtrue/appendAssemblyIddescriptorRefsdescriptorRefjar-with-dependencies/descriptorRef/descriptorRefs/configuration/pluginplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactId/plugin/plugins/build其实就是综合了SpringBoot运行的插件spring-boot-maven-plugin和maven打包插件maven-assembly-plugin两种方式结合使用大家可以直接复制
2、参考文献
《【Springboot】打包 Jar 出现 java.lang.NoClassDefFoundError 的问题》