Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java.lang.NoSuchMethodError:main。“线程中的异常”;“主要”;_Java - Fatal编程技术网

java.lang.NoSuchMethodError:main。“线程中的异常”;“主要”;

java.lang.NoSuchMethodError:main。“线程中的异常”;“主要”;,java,Java,我正在尝试从Java发送邮件 我得到这个运行时异常: java.lang.NoSuchMethodError:main。线程“main”中出现异常 我想不出是什么问题。请帮忙 我正在使用以下代码: import java.util.Properties; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Transport; i

我正在尝试从Java发送邮件

我得到这个运行时异常:

java.lang.NoSuchMethodError:main。线程“main”中出现异常

我想不出是什么问题。请帮忙

我正在使用以下代码:

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class Mail{
 public static void Mail(String from, String to, String subject, String text)
 {

  Properties props = new Properties();
  props.put("mail.smtp.host", "smtp.gmail.com");
  props.put("mail.smtp.port", "465");

  Session mailSession = Session.getDefaultInstance(props, null);
  Message simpleMessage = new MimeMessage(mailSession);

  InternetAddress fromAddress = null;
  InternetAddress toAddress = null;
  try
  {
   fromAddress = new InternetAddress(from);
   toAddress = new InternetAddress(to);
  } catch (AddressException e)
  {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

  try
  {
   simpleMessage.setFrom(fromAddress);
   simpleMessage.setRecipient(RecipientType.TO, toAddress);
   simpleMessage.setSubject(subject);
   simpleMessage.setText(text);

   Transport.send(simpleMessage);   
  } catch (MessagingException e)
  {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }  
 }

 public static void main(String[] args) 
 {

  String from = "prav.br@gmail.com";
  String to = "prav.br@gmail.com";
  String subject = "Test";
  String message = "A test message";

  Mail.Mail(from, to, subject, message);

 }
}
我认为代码中没有任何问题[至少它应该运行]。确保邮件api位于要编译的类路径中

建议:您的代码没有遵循java标准命名约定,您应该遵循该约定

另请参见


您必须创建方法
main(字符串[]args)

  • 您的方法称为Main而不是Main
  • 它接受多个参数,而不是1个字符串[]
  • 顺便说一句,java命名约定要求使用小写字母调用方法
    从您的示例中删除了所有JavaMail代码后,它编译和运行都没有问题。拥有一个与类同名的静态方法绝对是非常规的,我建议不要使用它,但它应该可以工作


    您尚未向我们提供任何详细信息或您的构建或执行环境。请这样做,并提供一个简短但完整的程序来演示这个问题-我建议您不要使用JavaMail的所有提示,因为这里不应该涉及这些提示。

    您能给我们堆栈跟踪或指示异常发生的行号吗?您的代码从此处开始工作,无需修改;javac-cp-path/to/java.jar-Mail.java-java-cp-path/to/javamail.jar:。MailThis Community Wiki问题列出了这个常见问题的可能原因:您误读了代码:第一种方法是Mail,而不是Main。。。在底部有一个单独的主方法。你是对的。我没有注意。这意味着他没有从其他地方得到任何错误。Praveen BR,给我们堆栈跟踪或自己查看。我相信您在IDE中有不同版本的JMail,在IDE中编译代码和运行代码