有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JAVANoClassDefFoundError:javax/mail/Authenticator,怎么了?

发送到电子邮件。java

package helper;

//Mail.java - smtp sending starttls (ssl) authentication enabled
//1.Open a new Java class in netbeans (default package of the project) and name it as "Mail.java"
//2.Copy paste the entire code below and save it.
//3.Right click on the file name in the left side panel and click "compile" then click "Run"

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;

public class sendToEmail
{
    String  d_email = "sample@gmail.com",
     d_password = "mysamplepassword",
     d_host = "smtp.gmail.com",
     d_port  = "465",
     //m_to = "sample@yahoo.com",
     m_subject = "trial",
     m_text = "Hey, this is the testing email.";

    public sendToEmail(String strEmailAddress)
    {


        Properties props = new Properties();
        props.put("mail.smtp.user", d_email);
        props.put("mail.smtp.host", d_host);
        props.put("mail.smtp.port", d_port);
        props.put("mail.smtp.starttls.enable","true");
        props.put("mail.smtp.auth", "true");
        //props.put("mail.smtp.debug", "true");
        props.put("mail.smtp.socketFactory.port", d_port);
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.socketFactory.fallback", "false");

        SecurityManager security = System.getSecurityManager();

        try
        {
            Authenticator auth = new SMTPAuthenticator();
            Session session = Session.getInstance(props, auth);
            //session.setDebug(true);

            MimeMessage msg = new MimeMessage(session);
            msg.setText(m_text);
            msg.setSubject(m_subject);
            msg.setFrom(new InternetAddress(d_email));
            msg.addRecipient(Message.RecipientType.TO, new InternetAddress(strEmailAddress));
            Transport.send(msg);
        }
        catch (Exception mex)
        {
            mex.printStackTrace();
        } 
    }

    public class SMTPAuthenticator extends javax.mail.Authenticator
    {
        public PasswordAuthentication getPasswordAuthentication()
        {
            return new PasswordAuthentication(d_email, d_password);
        }
    }
}

我的控制器的一部分。java

/* Send to Email will run properly soon */
            sendToEmail email = new sendToEmail(strEmailAddress);

当我运行web应用程序时,收到以下错误消息:

Type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception javax.servlet.ServletException: Servlet execution threw an exception

root cause java.lang.NoClassDefFoundError: javax/mail/Authenticator controller.RegisterTenantController.doPost(RegisterTenantController.java:108) javax.servlet.http.HttpServlet.service(HttpServlet.java:709) javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

我现在该怎么办?有人能帮我使这个web应用程序成功吗


共 (6) 个答案

  1. # 1 楼答案

    当我遇到这个问题时,我将mail-api.jar包含在我的maven pom文件中。这是API规范。解决方案是替换此:

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

    根据该api的参考实现:

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

    我知道包名中有sun,但那是最新版本。我是从https://stackoverflow.com/a/28935760/1128668学来的

  2. # 2 楼答案

    我曾经遇到过这种情况,我在类路径中有依赖项。解决方案是包含javax。邮件和javax。容器(例如tomcat)lib文件夹中的激活库。使用maven-将它们设置为提供的范围,它应该可以工作。您将在所有项目的类路径中共享电子邮件库

    有用的资料来源:http://haveacafe.wordpress.com/2008/09/26/113/

  3. # 3 楼答案

    将以下内容添加到maven依赖项中

        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4.5</version>
        </dependency>
    
        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>activation</artifactId>
            <version>1.1.1</version>
        </dependency>
    
  4. # 4 楼答案

    您需要将两个JAR添加到WEB-INF/lib目录或您的webapp(或服务器的lib目录)中:

  5. # 5 楼答案

    就连我也面临着类似的错误。尝试以下两个步骤(此处已经推荐了第一个步骤)—— 1.将依赖项添加到pom.xml

             <dependency>
                <groupId>javax.mail</groupId>
                <artifactId>mail</artifactId>
                <version>1.4.5</version>
            </dependency>
    
            <dependency>
                <groupId>javax.activation</groupId>
                <artifactId>activation</artifactId>
                <version>1.1.1</version>
            </dependency>
    
    1. 如果不起作用,请手动将jar文件放在.m2\repository\javax\<folder>\<version>\目录中
  6. # 6 楼答案

    虽然这可能是由于类路径中缺少jar文件造成的,但也可能不是

    在这种情况下,在我们的头脑中保持两个或三个不同的例外是很重要的:

    1. java.lang.ClassNotFoundException此异常表示在类路径上找不到该类。这表明我们试图加载类定义,而类路径上不存在该类

    2. java.lang.NoClassDefFoundError此异常表示JVM在其内部类定义数据结构中查找了类的定义,但没有找到它。这不同于说它不能从类路径加载。通常这表示我们以前试图从类路径加载类,但由于某种原因失败了-现在我们正在重试,但我们甚至不打算尝试加载它,因为我们之前加载失败。早期故障可能是ClassNotFoundExceptionExceptionInInitializerError(表示静态初始化块中出现故障)或任何数量的其他问题。关键是,NoClassDefFoundError不一定是类路径问题

    我将查看javax.mail.Authenticator的源代码,并查看它在静态初始值设定项中的作用。(查看静态变量初始化和静态块(如果有)如果在NoClassDefFoundError之前没有得到ClassNotFoundException,那么几乎可以保证这是一个静态初始化问题

    当hosts文件错误地定义了localhost地址,并且静态初始化块依赖于InetAddress.getLocalHost()时,我经常看到类似的错误。127.0.0.1应该指向“localhost”(也可能是localhost.localdomain)。它不应该指向机器的实际主机名(尽管出于某种原因,许多较老的RedHat Linux安装程序喜欢错误地设置它)