简体   繁体   English

由于messagingException尝试使用javamail运行.jar文件时出错

[英]Error when trying to run .jar file with javamail due to messagingException

I'm writing a little automated response program for my SO, and have incorporated an email function into it as a gimmick/test for myself. 我正在为我的SO编写一个小的自动响应程序,并将一个电子邮件功能合并到其中作为我自己的噱头/测试。 I have got the source code running fine in netbeans but when I try to run the .jar itself in the Mac terminal i get Error: Unable to initialize main class [class name] Caused by: java.lang.NoClassDefFoundError: javax/mail/MessagingException I am fairly new to java so I apologise for any vagueness or simplicity in the problem, but if there's anything that needs clarifying ill try my best 我已经在netbeans中运行了良好的源代码但是当我尝试在Mac终端中运行.jar时我得到错误:无法初始化主类[类名]引起:java.lang.NoClassDefFoundError:javax / mail / MessagingException我对java很新,所以我为问题中的任何模糊或简单道歉,但是如果有任何需要澄清的话,请尽我所能

I've tried just regularly running the .jar by double clicking and running it in terminal and neither works, but when i build and run the source code in netbeans, the program runs fine. 我试过通过双击并在终端中运行它来定期运行.jar并且都不起作用,但是当我在netbeans中构建并运行源代码时,程序运行正常。 I suspect it may to do with the catching exception line but I don't really know why. 我怀疑它可能与捕获异常行有关,但我不知道为什么。

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(myemail@gmail.com"));
                    //sets recipient as other email
        message.setRecipients(Message.RecipientType.TO,
            InternetAddress.parse("recipientemail@msn.com"));
                    //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);
    }

I think problem with running jar file from command line is that you don't have java.mail in your classpath, but when you run from IDE, it is added to project as external library. 我认为从命令行运行jar文件的问题是你的类路径中没有java.mail,但是从IDE运行时,它会作为外部库添加到项目中。 https://docs.oracle.com/javase/8/docs/technotes/tools/windows/classpath.html from @user999 来自@ user999的https://docs.oracle.com/javase/8/docs/technotes/tools/windows/classpath.html

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM