java.lang.NOSuchMethodError: main Exception in thread “main”

By Saruque Ahamed Mollick

There are lot of errors we have to face while programming. today we are going to know about this error mentioned below and also we will come to know that how to solve this kind of error

Reason behind the java.lang.NOSuchMethodError: main Exception in thread “main”:

Most of the new java programmers are known to this error message . many of them asked me actually what are the reasons behind this error and how can they resolve it.

So before going to resolve it we would suggest you to understand the reason behind java.lang.NoSuchMethodError: main Exception in thread “main” this error. If you understand the reason I hope you could resolve it yourself.

We use the command “java” to run a .java file . When you use the java command the command loads the class you mentioned in your program and then start searching for the main method or in easy way we can say that the compiler start looking for a method that usually looks like this :

public class anything {
...
public static void main (String[] args) {
   //body of main method ......
    }
}

So the main reason of this error is when you tried to compile your program but the code starts to call a method but the method is not available there or not existed on the class and the method might be static or non-static that does not matter at all.
On the above program the entry method is the main method which must have some minimum requirements . I would like to explain the requirements .
#1. The method must be in a class which is nominated before . (here the class is “anything”)

#2. We must have to make the main method public.



#3. We must have to make the main method static too .

#4. Return value must be a null value that means we have to make the return type void .

#5. We cant use more than one argument in this method . we need exactly one argument and the argument type must be String[] type

 

if any of the above requirements is not satisfied then we will find this error
java.lang.NoSuchMethodError: main Exception in thread “main”

or we can say this there are two main reason
1. The class which we are trying to run , there is no main method
2. there is incorrect signature of the main method .
lets see one example to understand it in a better way .

public class anything {
...
public static void main (String  args) {
   //body of main method ......
    }
}

now look at the code carefully we did not put [] this  . if we compile the code. we will get this error message

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

Leave a Reply

Your email address will not be published. Required fields are marked *