Java NoClassDefFoundError仅在通过命令行运行代码时发生
当我通过命令行运行这段代码时,我得到了一个例外。当我通过EclipseIDE运行它时,它运行良好,没有异常。我不知道发生了什么,但它肯定在会话声明中得到了例外Java NoClassDefFoundError仅在通过命令行运行代码时发生,java,eclipse,command-line-interface,noclassdeffounderror,Java,Eclipse,Command Line Interface,Noclassdeffounderror,当我通过命令行运行这段代码时,我得到了一个例外。当我通过EclipseIDE运行它时,它运行良好,没有异常。我不知道发生了什么,但它肯定在会话声明中得到了例外 public static void sendEmail(String sendFile) { // Send the log file via email. final String username = "firstname.lastname@mycompany.com"; final String passw
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();
}
}
只有在命令行上运行时,才会出现错误:
线程“main”java.lang.NoClassDefFoundError中的异常:com/sun/mail/util/MailLogger
位于javax.mail.Session.initLogger(Session.java:221)
在javax.mail.Session.(Session.java:206)
在javax.mail.Session.getInstance(Session.java:242)
在VerifyUrl.sendmail(VerifyUrl.java:186)上
位于VerifyUrl.main(VerifyUrl.java:50)
原因:java.lang.ClassNotFoundException:com.sun.mail.util.MailLogger
在java.net.URLClassLoader$1.run(URLClassLoader.java:366)
在java.net.URLClassLoader$1.run(URLClassLoader.java:355)
位于java.security.AccessController.doPrivileged(本机方法)
位于java.net.URLClassLoader.findClass(URLClassLoader.java:354)
位于java.lang.ClassLoader.loadClass(ClassLoader.java:425)
位于java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 还有5个
你知道这是什么原因吗?这是java开发人员面临的最常见问题
- 其中java具有包结构机制李>
- 要运行任何使用其他包类/接口/枚举的java程序,需要将其导入到类中李>
- 如果您使用的是外部库或其他软件包,则需要在您的案例中为该类设置类路径
com.sun.mail.util.MailLogger
- 像eclipse和IntellijIdea这样的IDE为您打包和设置类路径
- 如果您试图运行上述程序或任何其他程序,您可以遵循如下语法
你的情况是这样的java-cp/relative\u path\u to\u your\u dependency\u 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环境中(这就是为什么您可以在live server上使用它),但它不包含在SE环境中。我是Java初学者,所以我不理解您所说的1%。我已经尝试了所有我能理解的链接,但我仍然有这个问题。如果有人能用通俗易懂的英语说出来,我将不胜感激。请不要在没有告诉我XML应该放在什么文件和它的位置的情况下发布XML内容。同样,我的类路径很好。它在Eclipse中工作得很好。Eclipse中包含的任何内容都在我的类路径中。我已经删除了javaxapijar文件,而是包含javax.mail.jar文件。仍然不起作用。而且它没有丢失activation.jar。我刚刚将它添加到我的类路径中,仍然得到相同的错误。请您至少添加整个类的import语句好吗