【问题标题】:NoClassDefFoundError only happening when running code via command lineNoClassDefFoundError 仅在通过命令行运行代码时发生
【发布时间】:2016-12-19 19:09:33
【问题描述】:

当我通过命令行运行此代码时出现异常。当我通过 Eclipse IDE 运行它时,它运行良好,没有例外。我不知道发生了什么,但肯定会在 Session 语句中出现异常。

public static void sendEmail(String sendFile) {
    // Send the log file via email.
    final String username = "firstname.lastname@mycompany.com";
    final String password = "myPassword";

    // Strings that contain from, to, subject, body and file path to the attachment.
    String from = username;
    String to = username;
    String subject = "Baggage URL failures - Please check";
    String body = "FareDB Bad URL(s) Report. The following URL(s) could not be loaded...";
    String filename = sendFile;
    System.out.println("Mail header info set!");

    // Set smtp properties
    Properties properties = new Properties();
    properties.put("mail.smtp.starttls.enable", "true");
    properties.put("mail.smtp.auth", "true");
    properties.put("mail.smtp.host", "smtp.gmail.com");
    properties.put("mail.smtp.port", "587");
    System.out.println("SMTP properties set!");

    Session session = Session.getInstance(properties, null);
    System.out.println("Mail session declared!");

    try {
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        // Just me for testing
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(username));
        message.setSubject(subject);
        message.setSentDate(new Date());
        System.out.println("Mail body info set!");

        // Set the email body
        MimeBodyPart messagePart = new MimeBodyPart();
        messagePart.setText(body);
        System.out.println("Mime message body set!");

        // Set the email attachment file
        MimeBodyPart attachmentPart = new MimeBodyPart();
        FileDataSource fileDataSource = new FileDataSource(filename) {
            @Override
            public String getContentType() {
                return "application/octet-stream";
            }
        };
        attachmentPart.setDataHandler(new DataHandler(fileDataSource));
        attachmentPart.setFileName(fileDataSource.getName());
        System.out.println("Mail attachment info set!");

        // Add all parts of the email to Multipart object
        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(messagePart);
        multipart.addBodyPart(attachmentPart);
        message.setContent(multipart);
        System.out.println("Mail parts added!");

        // Send email
        Transport.send(message);
        System.out.println("Mail sent!");
    } catch (MessagingException e) {
        e.printStackTrace();
    }
}

我得到的错误只是在命令行运行时:

 Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/MailLogger
    at javax.mail.Session.initLogger(Session.java:221)
    at javax.mail.Session.<init>(Session.java:206)
    at javax.mail.Session.getInstance(Session.java:242)
    at VerifyUrl.sendEmail(VerifyUrl.java:186)
    at VerifyUrl.main(VerifyUrl.java:50)
 Caused by: java.lang.ClassNotFoundException: com.sun.mail.util.MailLogger
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 5 more

有什么可能导致这种情况的想法吗?

【问题讨论】:

标签: java eclipse command-line-interface noclassdeffounderror


【解决方案1】:

这是 Java 开发人员面临的最常见问题。

  • 其中java有包结构机制。
  • 要运行任何使用其他包类/接口/枚举的 java 程序,您需要将其导入类。
  • 如果您使用的是外部库或其他包,那么您需要在您的情况下为该类设置类路径com.sun.mail.util.MailLogger
  • Eclipse 和 IntellijIdea 等 IDE 为您打包和设置类路径。
  • 如果您尝试运行上述程序或任何其他程序,您可以遵循类似

    的语法

    java -cp /relative_path_to_your_dependency_jars ClassName

    在你的情况下是这样的。

    java -cp /downloadpath/javax.mail-1.5.0.jar YouMainClass

    类似的编译类

    java -cp /downloadpath/javax.mail-1.5.0.jar YouMainClass.java

【讨论】:

  • 就是这样,我的类路径设置正确。我得继续看这个。这根本没有意义。
  • com.sun.mail.util.MailLogger 是 JavaMail API 的一部分。它已经包含在 EE 环境中(这就是您可以在实时服务器上使用它的原因),但它不包含在 SE 环境中。
  • 我是 Java 的初学者,所以我不明白你所说的 1%。我已经尝试了上面发布的链接中可以理解的所有内容,但我仍然遇到问题。如果有人可以用简单的英语表达,我将不胜感激。请不要在不告诉我它应该进入什么文件以及它位于何处的情况下发布 XML 内容。同样,我的类路径很好。它在 Eclipse 中运行良好。我在 Eclipse 中包含的所有内容都在我的类路径中。我已经删除了 javax-api jar 文件,而是包含了 javax.mail.jar 文件。还是不行。
  • 而且它没有缺少activation.jar。我刚刚将它添加到我的类路径中,但仍然出现相同的错误。
  • 请您至少添加整个类的强制执行导入语句
【解决方案2】:

这原来是一个类路径问题,并且还使用了 javax.mail 而不是 javax.mail-api。

问题已经解决了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多