Tuesday, November 18, 2008

Published 11/18/2008 by with 0 comment

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

'Exception in thread "main" java.lang.NoSuchMethodError: main' is a run time error message. If the main method is not properly defined, we may see this error message. You know if you want to execute/run a program that program must have a main method of the following signature:

public static void main(String args[])

Generally we do the following mistakes in this regard:

1. May be, you haven't defined the main method at all. Define it at first then run it.

2. The method name is not correctly written. The spelling will be exactly in lower case 'main', if we write 'Main' - it will be an error.

For example:

public class NoSuchMethodErrorMain
{
public static void Main(String args) // M is capital here
{
System.out.println("Java is an Object Oriented Language");
}

}




3. The keyword 'static' is omitted. The main method must be static.

For example,

public class NoSuchMethodErrorMain
{
public void main(String args) // keyword static is missing
{
System.out.println("Java is an Object Oriented Language");
}

}



4. The parameter is not declared. Java main method must have a String array parameter. Sometimes, we forget to put [] after the data type or the parameter name (args, here).

For example:

public class NoSuchMethodErrorMain
{
public static void main() // parameter problem
{
System.out.println("Java is an Object Oriented Language");
}

}


OR

public class NoSuchMethodErrorMain
{
public static void main(String args) // the parameter is not array
{
System.out.println("Java is an Object Oriented Language");
}

}


The correct code will be:

public class NoSuchMethodErrorMainSolution
{
public static void main(String args[])
{
System.out.println("Java is an Object Oriented Language");
}

}
    email this       edit

0 comments:

Post a Comment