-1

Possible Duplicate:
Exception in thread “main” java.lang.NoSuchMethodError: main

public class m
{
    int a; //class variable


    void f1()
    {
       int b=10;
       System.out.println(a);
       System.out.println(b);
    }
}
class B
{
   public static void main(String args[])
   {
      m ob=new m(); //object created
      ob.f1(); //calling f1 method

   }
}
Community
  • 1
  • 1
munna
  • 304
  • 5
  • 19
  • 4
    Hi, welcome at Stackoverflow. We would appreciate if you take a *bit* more effort in asking the question the smart way in the future :) – BalusC Jun 04 '10 at 02:18
  • 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:37

3 Answers3

5

I'll guess.

You are trying to invoke:

java m

Since you defined you main method in class B you should call

java B

To execute it.

Here's my test:

$cat >m.java<<. 
> public class m
> {
>     int a; //class variable
>     void f1()
>     {
>        int b=10;
>        System.out.println(a);
>        System.out.println(b);
>     }
> }
> class B
> {
>    public static void main(String args[])
>    {
>       m ob=new m(); //object created
>       ob.f1(); //calling f1 method
>    }
> }
> .
$javac m.java 
$java m 
Exception in thread "main" java.lang.NoSuchMethodError: main
$java B
0
10
$

If you see, invoking java B prints 0 10as expected.

OscarRyz
  • 184,433
  • 106
  • 369
  • 548
2

Main needs to be in the top-level class whose name corresponds to the filename - so if "m" is the name of your file that's where main needs to be. Note that by convention class names start with an uppercase letter.

JRL
  • 72,358
  • 17
  • 90
  • 140
  • Not necessarily, the `main` method could be in class `B` it is just, `B` needs to be invoked not `m` – OscarRyz Jun 04 '10 at 02:32
1

Did you invoke the Java program with java m? The main method is defined in the class B, not m, therefore, you need the java B command to invoke it. It may be confusing if you reasoned by "Same as filename.".

Also, putting two classes in one file may be a bad practice.

Ming-Tang
  • 16,475
  • 8
  • 35
  • 73