简体   繁体   中英

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

I'm getting below error while sending email from a java web project.
IDE: eclipse
Java version: openjdk version "1.8.0_212-3-redhat"
Spring version: 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)

Following is the filtered dependency tree for mail
在此处输入图像描述


I have tried the following:-

  • Maven install
  • Deleted existing tomcat server and added a new server in eclipse.
  • changed local repository of maven for fresh dependencies download.
  • deleted the project from eclipse workspace and deleted all eclipse settings and configuration file/folder then re-import the existing maven project
  • checked for org.springframework.mail.javamail.SmartMimeMessage class in the spring-context-support-5.1.6.RELEASE.jar at project explorer as well as on file system
    在此处输入图像描述

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.

There are package and class names collision among the following jars:-

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

Problem cause :
Both jars will be part of the build and all classes will be added to the classpath.
Now the classes at the mercy of class load order and causing different version than expected.

What triggers the problem :
I have run the maven with force update of Snapshots/Releases
Now the latest version 4.10.0 of the following dependency has jakarta.mail-api.jar as an internal dependency.

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

Solution : Use only one jar. In may case javax.mai-api.jar
What to change : I have added the exclusion for the jakarta.mail-api.jar in pom.xml

<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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM