1

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.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);

 }
}
Cœur
  • 32,421
  • 21
  • 173
  • 232
Praveen BR
  • 11
  • 1
  • 2
  • 1
    can you give us the stack trace or indicate the line number where exception occurs – lweller Jan 18 '11 at 11:55
  • 2
    your code works from here without modification; *javac -cp path/to/java.jar Mail.java* - *java -cp path/to/javamail.jar:. Mail* – guido Jan 18 '11 at 12:01
  • This Community Wiki question lists the possible causes of this common problem: http://stackoverflow.com/questions/5407250/causes-of-java-lang-nosuchmethoderror-main-exception-in-thread-main – Stephen C Jun 28 '11 at 14:38

3 Answers3

2

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:

jmj
  • 225,392
  • 41
  • 383
  • 426
1

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.

Jon Skeet
  • 1,261,211
  • 792
  • 8,724
  • 8,929
-3

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.
AlexR
  • 109,181
  • 14
  • 116
  • 194
  • 1
    You've misread the code: the first method is Mail, not Main... and there's a separate main method at the bottom. – Jon Skeet Jan 18 '11 at 11:58
  • You are right. I did not pay attention. This means that he got NoSuchMethodError from other place. Praveen BR, give us stack trace or review it yourself. I believe that you have different version of JMail in IDE where you compile code and when you are running it. – AlexR Jan 18 '11 at 12:01