NoSuchMethodError is thrown when a Java class trying to invoke a method from another class which is not exist. Typically, this error has to be caught at the compile time. However, this error also thrown at run time when class has incompatibly changed. This error is sub class of java.lang.IncompatibleClassChangeError. This class inherited from the LinkageError super class.

Look at the below class without main method:

public class HelloError {
	//No Main method
	public void method(){


When you run the above program, you would get the below exception:

Exception in thread "main" java.lang.NoClassDefFoundError: HelloError (wrong nam
e: javabeat/net/util/HelloError)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at Source)
        at Source)
        at$100(Unknown Source)
        at$ Source)
        at$ Source)
        at Method)
        at Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)

The requirement for the main method is :

  • The method must be exist in the class.
  • The name of the method must be exactly “main”.
  • The method must be public.
  • The method must be static.
  • The method’s return type must be void.
  • The method must have exactly one argument, and the type of that argument must be String[].

But, in the real scenarios the error will be thrown for the many other situations not just for the main method. When you have the wrong JAR files which don’t have the correct version of the method which you are invoking.



About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind