java.lang.NoSuchMethodError Example

java.lang.NoSuchMethodError is being thrown when program tries to call a class method that doesn’t exist. The method can be static or it can be an instance method too.



Most of the times java.lang.NoSuchMethodError is caught be compiler but sometimes it can occur at runtime. If this error occurs at runtime then the only reason could be the change in the class structure that made it incompatible.

Let’s try to raise this error at runtime. So we have two classes Data and Temp as shown below.

Both the programs look fine, let’s compile and run them through command line. Note that I am not using Eclipse or any other IDE to avoid compile time detection of this error when I will change the Data class later on.

So program executed fine, let’s go ahead and change Data class definition as shown below.

Notice that I have removed bar() method. Now we will have to compile only Data class, don’t compile the main class.

As you can see that we have got the java.lang.NoSuchMethodError because the Data class became incompatible with Temp class. If we would have tried to compile Data class, we would have got compile time error as shown below.

java.lang.NoSuchMethodError in Tomcat

Most of the time you will see java.lang.NoSuchMethodError in some applications with many dependencies. There are two main reasons for that;

  1. Jar version used at the compile time is different from the runtime. For example, you might have MySQL jar in your application having different version from what is present in Tomcat lib folder. Since tomcat lib folder jars are looked first by Java Classloader, there is a chance of java.lang.NoSuchMethodError if any method is not found in the class loaded.
  2. Conflict because of same class name. For example, your application is using some third party jar which has same fully qualified class name as yours. So if classloader loads the class from other jar, you will get java.lang.NoSuchMethodError at runtime

Debugging java.lang.NoSuchMethodError

You can use java runtime option -verbose:class to get the information about the jar that is used to load a class. You can set this configuration in tomcat or like below.

Then you will see logs like below that is very helpful to figure out the actual jar used in loading a class and the reason for java.lang.NoSuchMethodError at runtime.

That’s all for java.lang.NoSuchMethodError example and tips to detect and fix it. I hope it will be helpful to you in getting rid of this error.

Reference: API Document

By admin

Leave a Reply

%d bloggers like this: