java.lang.NoSuchMethodError

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:

package javabeat.net.util;
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 java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$100(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown 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.

Comments

comments

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

*