java.lang.NoClassDefFoundError: Could not initialize class org.springframework.mail.javamail.SmartMimeMessage

java.lang.NoClassDefFoundError: Could not initialize class org.springframework.mail.javamail.SmartMimeMessage

我从 java 网络项目发送电子邮件时遇到以下错误。
IDE: 日食
Java版本:openjdk版本“1.8.0_212-3-redhat”
Spring版本:5.1.6.RELEASE

java.lang.NoClassDefFoundError: Could not initialize class org.springframework.mail.javamail.SmartMimeMessage
at org.springframework.mail.javamail.JavaMailSenderImpl.createMimeMessage(JavaMailSenderImpl.java:340)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:373)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:365)

以下是过滤后的邮件依赖关系树


我尝试了以下方法:-

NOTE: This was a working code but suddenly I'm getting this error now.
No Compilation error in project and war is also able to successfully deployed on the server.

以下 jar 之间存在包和 class 名称冲突:-

  • jakarta.mail-api.jar
  • javax.mail-api.jar

问题原因:
两个 jar 都将成为构建的一部分,所有 classes 都将添加到 classpath .
现在 classes 受 class 加载 order 的支配,导致版本与预期不同。

是什么触发了问题
我有 运行 maven 强制更新 Snapshots/Releases
现在,以下依赖项的最新版本 4.10.0 具有 jakarta.mail-api.jar 作为内部依赖项。

<dependency>
    <groupId>com.google.api-ads</groupId>
    <artifactId>ads-lib</artifactId>
    <version>RELEASE</version>
</dependency> 

解决方法:只用一个罐子。在可能的情况下 javax.mai-api.jar
要更改的内容:我在 pom.xml

中添加了 jakarta.mail-api.jar 的排除项
<dependency>
    <groupId>com.google.api-ads</groupId>
    <artifactId>ads-lib</artifactId>
    <version>RELEASE</version>
    <exclusions>
        <exclusion>
            <groupId>jakarta.mail</groupId>
            <artifactId>jakarta.mail-api</artifactId>
        </exclusion>
    </exclusions>
</dependency>

Key learning point : Use of Release/Snapshot version in pom.xml can lead to dependency conflicts and issues like NoClassDefFoundError if you run the maven with option force update of Snapshots/Releases