-1
import java.io.*;
import java.lang.Math;
class Squr
{
  public static void main ()
  { 
   int m =10,n;
   double z = 10.4,p;
   Squr square = new Squr();
   p = (double)square.mysqrt(z);
   n = (int)square.mysqrt(m);
   System.out.println ("square root of 10 : " + n );
   System.out.println ("square root of 10.4 : "+ p );  
  }
    double mysqrt (double y)
   {
     return Math.sqrt(y);
   }
   int mysqrt (int x)
   {
     return (int)Math.sqrt(x);
   }

}

This code is compiling but when we try to execute it it giving " Exception in thread "main" Java.lang.NoSuchMethodError: main "

Nishant Kumar
  • 5,995
  • 19
  • 69
  • 95
  • 1
    Pay (close) attention to the error messages and full-type signatures. Happy coding. –  Jan 28 '11 at 11:45
  • 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:39

6 Answers6

7

The main() function should be declared like this

public static void main(String[] args)
nhahtdh
  • 55,989
  • 15
  • 126
  • 162
DoctorRuss
  • 1,429
  • 10
  • 9
4

The correct method signature for the main method in Java is:

public static void main(String args[])

Simply add the missing arguments in you method declaration and it should work.

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
2

Try that:

public static void main(String [ ] args)
nrph
  • 335
  • 7
  • 18
2

It looks like you've not defined your main method with the correct signature. It should be:

public class Squr
{
  public static void main(String[] args)
John Pickup
  • 5,015
  • 2
  • 22
  • 15
2

Your main() method should be like that

public static void main(String args[])

or

public static void main(String[] args)

or

public static void main(String... args)
  • Above two suggestion public static void main(String args[]) and public static void main(String[] args) is working well but not third one... – Nishant Kumar Jan 28 '11 at 10:27
  • It should work. It works for me. Here is demo http://ideone.com/OhZvh –  Jan 28 '11 at 10:33
  • Note that the 3rd option will only compile when using Java 5+ as that is when the varargs (...) notation was introduced. – Michael Rutherfurd Feb 08 '11 at 23:39
1

Java is a strong type language. You must declare the method in a given way. The right way to define the main() method is:

public static void main (String[] args)
user unknown
  • 35,537
  • 11
  • 75
  • 121
Bhanu
  • 11
  • 2
  • There are already 5, nearly identical answers. Can you point out what is new in your answer? Couldn't you comment on one of the other posts? If you agree with another post, vote it up, but don't repeat it, especially not 3 months later. Thank you. – user unknown May 04 '11 at 17:07