我正在为我的SO编写一个小的自动响应程序,并将一个电子邮件功能合并到其中作为我自己的噱头/测试。我已经在netbeans中运行了良好的源代码但是当我尝试在Mac终端中运行.jar时我得到错误:无法初始化主类[类名]引起:java.lang.NoClassDefFoundError:javax / mail / MessagingException我对java很新,所以我为问题中的任何模糊或简单道歉,但是如果有任何需要澄清的话,请尽我所能
我试过通过双击并在终端中运行它来定期运行.jar并且都不起作用,但是当我在netbeans中构建并运行源代码时,程序运行正常。我怀疑它可能与捕获异常行有关,但我不知道为什么。
try{
//sets variables to store custom subject and body data
System.out.print("Subject: ");
//input is name of scanner
String subject=input.next();
System.out.print("Body: ");
String body=input.next();
Message message = new MimeMessage(session);
//sets sender as my email
message.setFrom(new InternetAddress([email protected]"));
//sets recipient as other email
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("[email protected]"));
//sets subject and content as variables defined earlier
message.setSubject(subject);
message.setText(body);
//sends message
Transport.send(message);
System.out.println("Done");
//catches and throws a necessary messaging exception
} catch (MessagingException e) {
throw new RuntimeException(e);
}
我认为从命令行运行jar文件的问题是你的类路径中没有java.mail,但是从IDE运行时,它会作为外部库添加到项目中。来自@ user999的https://docs.oracle.com/javase/8/docs/technotes/tools/windows/classpath.html