What is the difference between java.lang.NoSuchMethodError main Exception in thread "main" and Error: main method not found in class

I have this class

public class demo3 {
    private static  void sum()
    {       
    }
}

      

when i tried to run this class i expected the error to be java.lang.NoSuchMethodError main Exception in thread "main "

however, the result was slightly different and I got below post

Error: Main method not found in class demo3, please define the main method as:
   public static void main(String[] args)

      

now this piqued my curiosity as to in which case I would get java.lang.NoSuchMethodError or in which case I would get a different error message.

+3


source to share


1 answer


You get a message Main method not found

when public static void main(String[])

it cannot be found in the class that you asked the JVM to start working on. That is, the entry point to the general program cannot be found.

You get a message java.lang.NoSuchMethodError

if your (already running) code tries to call a class method that was available at compile time but is not available in the version of the class you are using at runtime (for example, you compile one version of a library and then update the library jar without recompilation). This can happen at any point in the program.



There should be nothing in the JLS that says something that NoSuchMethodError

cannot be selected, rather than Main method not found

; however, not being able to write a method main

(either completely or writing one with the wrong signature) is a much more common mistake than the "class changed since compilation" case, especially for newbies who NoSuchMethodError

can be too cryptic for. There is no harm in providing a more user-friendly message in this case.

+2


source







All Articles