简体   繁体   English

java.lang.NoClassDefFoundError:无法初始化类javax.mail.internet.InternetAddress

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

I am trying to send an email using the JavaMail API. 我正在尝试使用JavaMail API发送电子邮件。 Here is my code on the servlet: 这是我关于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 java.lang.NoClassDefFoundError:无法初始化类javax.mail.internet.InternetAddress

In pom.xml, I added these lines: 在pom.xml中,我添加了以下几行:

<!--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. 我正在使用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. 您将得到一个java.lang.NoClassDefFoundError,这意味着JVM无法初始化该类,而不是它找不到将为ClassNotFoundException的类。 A NoClassDefFoundError can be caused by a ClassNotFoundException but that need not be the case. NoClassDefFoundError可以由ClassNotFoundException引起,但不必如此。

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. 如果幸运的话,这就是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. 在构建时添加依赖项并不能使依赖项在运行时可用于Tomcat。 You need to add the javax.mail.jar file to the WEB-INF/lib directory of your application, or to Tomcat's lib directory. 您需要将javax.mail.jar文件添加到应用程序的WEB-INF / lib目录或Tomcat的lib目录中。

Of course, you wouldn't have this problem if you were using a full Java EE application server instead of Tomcat... :-) 当然,如果您使用完整的Java EE应用程序服务器而不是Tomcat,就不会有此问题... :-)

Please see: https://stackoverflow.com/a/28935760/1128668 You have included the mail-api.jar in your project. 请参阅: https : //stackoverflow.com/a/28935760/1128668您已在项目中包含mail-api.jar That's the API specification only . 只是 API规范。 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: 与该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. 我知道程序包名称中包含sun,但这是最新版本。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 javax.mail.internet.InternetAddress本地部分的UTF-8 - javax.mail.internet.InternetAddress UTF-8 in local-part javax.mail.internet.InternetAddress RFC 822验证 - javax.mail.internet.InternetAddress RFC 822 validation java.lang.NoClassDefFoundError:无法初始化类 - java.lang.NoClassDefFoundError: Could not initialize class java.lang.NoClassDefFoundError:无法初始化类 - java.lang.NoClassDefFoundError: Could not initialize class 错误:无法初始化主 class AirMail 原因:java.lang.NoClassDefFoundError: javax/mail/Authenticator - Error: Unable to initialize main class AirMail Caused by: java.lang.NoClassDefFoundError: javax/mail/Authenticator java.lang.NoClassDefFoundError:无法初始化类javax.swing.UIManager - java.lang.NoClassDefFoundError: Could not initialize class javax.swing.UIManager java.lang.NoClassDefFoundError:无法初始化类javax.media.jai.JAI - java.lang.NoClassDefFoundError: Could not initialize class javax.media.jai.JAI java.lang.NoClassDefFoundError:无法初始化类javax.imageio.ImageIO jboss EAP 6 - java.lang.NoClassDefFoundError: Could not initialize class javax.imageio.ImageIO jboss EAP 6 java.lang.NoClassDefFoundError:javax / mail / MessagingException - java.lang.NoClassDefFoundError: javax/mail/MessagingException TomEE:javax.servlet.ServletException:java.lang.NoClassDefFoundError:无法初始化类javax.ws.rs.core.UriBuilder - TomEE: javax.servlet.ServletException: java.lang.NoClassDefFoundError: Could not initialize class javax.ws.rs.core.UriBuilder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM