简体   繁体   中英

java.lang.NoClassDefFoundError: Could not initialize class javax.mail.internet.InternetAddress

I am trying to send an email using the JavaMail API. Here is my code on the servlet:

package com.lsp.web;

import com.lsp.service.Mailer;
import javax.ejb.EJB;
import javax.mail.MessagingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "contact", urlPatterns = {"/contact"})
public class ContactServlet extends SpringInjectedServlet {
@EJB
private Mailer emailBean;

@Override
public void init() throws ServletException {

}

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    doPost(req, resp);
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String customerEmail = req.getParameter("email");
    String subject = req.getParameter("subject");
    String body = req.getParameter("message");

    String error = null;
    String succMess = null;

    try {
        javax.mail.internet.InternetAddress ia = new javax.mail.internet.InternetAddress(customerEmail);
        ia.validate();
        emailBean.send(customerEmail, subject, body);
        req.setAttribute("succMessage", succMess);
        req.getRequestDispatcher("sent.jsp").forward(req, resp);

    } catch (javax.mail.internet.AddressException ae) {
        error = "您指出的邮箱地址不存在";
        req.setAttribute("errorMessage", error);
        req.getRequestDispatcher("contact.jsp").forward(req, resp);
    }
    catch (MessagingException mex) {
        error = "发送失败";
        req.setAttribute("errorMessage", error);
        req.getRequestDispatcher("contact.jsp").forward(req, resp);
    }
}
}

At the line where I check for the user address where:

javax.mail.internet.InternetAddress ia = new javax.mail.internet.InternetAddress(customerEmail);
ia.validate();

I got an exception. java.lang.NoClassDefFoundError: Could not initialize class javax.mail.internet.InternetAddress

In pom.xml, I added these lines:

<!--JavaMail API-->
    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>javax.mail-api</artifactId>
        <version>1.5.1</version>
    </dependency>

    <!--EJB-->
    <dependency>
        <groupId>javax.ejb</groupId>
        <artifactId>ejb-api</artifactId>
        <version>3.0</version>
    </dependency>

I am using Tomcat. Could someone tell me why this happens and how I can solve the issue.

Thank you.

You get a java.lang.NoClassDefFoundError, which means that the JVM can't initialise the class, not that it can't find the class which would be a ClassNotFoundException. A NoClassDefFoundError can be caused by a ClassNotFoundException but that need not be the case.

In order to find the cause, stop the server, delete the log and start again. Then reproduce the error and try to find the first Exception and its cause in your log file. If you are lucky this is the cause for the NoClassDefFoundError.

You also might indicate in your question which server you are using. It might make a difference how to solve the error.

Adding the dependency at build time does nothing to make the dependency available to Tomcat at runtime. You need to add the javax.mail.jar file to the WEB-INF/lib directory of your application, or to Tomcat's lib directory.

Of course, you wouldn't have this problem if you were using a full Java EE application server instead of Tomcat... :-)

Please see: https://stackoverflow.com/a/28935760/1128668 You have included the mail-api.jar in your project. That's the API specification only . The fix is to replace this:

<!-- DO NOT USE - it's just the API, not an implementation -->
<groupId>javax.mail</groupId>
<artifactId>javax.mail-api</artifactId>

with the reference implementation of that api:

<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>

I know it has sun in the package name, but that's the latest version.

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