开发者

java.lang.NoSuchMethodError: main. Exception in thread "main" [duplicate]

This question already has answers here: "Error: Main method not found in class MyClass, please define the main method as..." (10 answers) Closed 9 years ago.

I'm trying to mail from Java.

I'm getting this runtime exception:

java.lang.NoSuchMethodError: main. Exception in thread "main"

Cant figure out what the problem is. Please help.

I'm using the following code:

import java.util.Properties;
import javax.mail开发者_Go百科.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);

 }
}


I don't see any problem in code[atleast it should run] . make sure mail api is in classpath to compile.

Suggestion : your code doesn't follow java standard naming convention you should follow that.

Also See:

  • Coding Convention


Having removed all the JavaMail code from your sample, it compiles and runs with no problems. Having a static method with the same name as the class is definitely unconventional and I would advise against it, but it should work...

You haven't given us any details or your build or execution environment. Please do so, and provide a short but complete program which demonstrates the problem - I suggest you get rid of all hints of JavaMail as that shouldn't be involved here.


You have to create method main(String[] args)

  1. Your method is called Main instead of main.
  2. It accepts several arguments instead of 1 String[]
  3. BTW java naming conventions requires calling methods using small letters.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜