笔记

万物寻其根,通其堵,便能解其困。
  博客园  :: 新随笔  :: 管理

打包相关处理异常

Posted on 2025-02-13 11:29  草妖  阅读(7)  评论(0)    收藏  举报

警告[收录未验证]:Unable to process Jar entry [module-info.class] from Jar [具体jar包路径] for annotations

在 Tomcat 中运行 Spring Boot 项目时,如果遇到以下错误:Unable to process Jar entry [module-info.class] from Jar [具体jar包路径] for annotations
󠁪这是由于某些版本的 Tomcat 或 Java 不支持 module-info.class 文件导致的。以下是问题的原因分析及解决方法。
原因分析
1. 模块化系统引入:
  •Java 9 引入了模块化系统(JPMS),新增了 module-info.class 文件用于定义模块。
  •某些旧版本的 Tomcat 或工具(如 Servlet 容器中的类加载器)可能无法正确处理 module-info.class 文件。
2. Spring Boot 的兼容性:
  •如果使用的 Spring Boot 版本较新,而 Tomcat 版本较旧,则可能导致不兼容的问题。
  •某些第三方依赖库可能包含 module-info.class 文件,Tomcat 在扫描这些文件时会报错。
3. Tomcat 的类加载机制:
  •Tomcat 在启动时会扫描所有 JAR 包中的注解(例如 @WebServlet、@Controller 等),但如果遇到不支持的文件类型(如 module-info.class),就会抛出此错误。
解决方法
方法一:升级 Tomcat 和 JDK
  •升级 Tomcat:
    •确保使用的是支持 Java 9+ 的 Tomcat 版本(如 Tomcat 8.5+ 或更高版本)。
    •推荐使用与 Spring Boot 版本匹配的 Tomcat 版本。
  •升级 JDK:
  •使用与项目兼容的 JDK 版本(如 JDK 11 或 JDK 17),避免使用过低版本的 JDK。
方法二:禁用注解扫描
  可以通过配置禁用对 module-info.class 的扫描,从而避免该错误。
  1. 修改 context.xml 文件:
    •打开 Tomcat 配置文件 $TOMCAT_HOME/conf/context.xml。
    •添加或修改以下内容:
      <Context>       <Loader scanClassPath="false" />       </Context>  
      •参数 scanClassPath="false" 禁用了对类路径中注解的扫描。
  2. 通过代码禁用扫描:
    •在 Spring Boot 应用的 application.properties 或 application.yml 中添加以下配置:
      spring.jmx.enabled=false       spring.main.allow-bean-definition-overriding=true     •这可以减少不必要的类加载和扫描操作。
方法三:排除问题 JAR 包
  如果问题是由某个特定的第三方依赖引起的,可以通过排除该依赖来解决问题。
  1. 检查问题 JAR 包:
    •根据错误日志中的路径定位到具体的 JAR 包。
    •如果该 JAR 包不是必须的,可以在 pom.xml 中排除它:
       <dependency>       <groupId>com.example</groupId>       <artifactId>example-library</artifactId>       <version>1.0.0</version>       <exclusions>       <exclusion>       <groupId>*</groupId>       <artifactId>*</artifactId>       </exclusion>       </exclusions>       </dependency>
  2. 降级依赖版本:
    •如果该 JAR 包是必须的,尝试使用一个不包含 module
-info.class 的旧版本。
方法四:使用 Spring Boot 提供的嵌入式 Tomcat
  •如果问题难以解决,可以直接使用 Spring Boot 内置的嵌入式 Tomcat,而不是外部部署到独立的 Tomcat 容器。
  •修改 pom.xml 或 build.gradle,确保依赖中包含嵌入式 Tomcat:
    <dependency>     <groupId>org.springframework.boot</groupId>     <artifactId>spring-boot-starter-tomcat</artifactId>     <scope>provided</scope>     </dependency> 总结
  •推荐方案:优先升级 Tomcat 和 JDK 至最新稳定版本,确保兼容性。
  •快速解决:通过禁用注解扫描(方法二)或排除问题 JAR 包(方法三)快速修复问题。
  •长期建议:尽量使用 Spring Boot 提供的嵌入式容器,减少与外部 Tomcat 的兼容性问题。
完成上述调整后,重启 Tomcat 即可验证问题是否解决。

 

部分服务器会出现:“Handler dispatch failed; nested exception is java.lang.StackOverflowError”,具体可看(JAVA)一处编译,(BUG)到处运行 之 常用错误记录/常用笔记 - 闪电龟龟 - 博客园 笔记,查询“Handler dispatch failed; nested exception is java.lang.StackOverflowError”

待续....