How to Solve java.lang.NoSuchMethodError in Java?

28 Jul, 2021
A java.lang.NoSuchMethodError as the name suggests, is a runtime error in Java which occurs when a method is called that exists at compile-time, but does not exist at runtime. The java.lang.NoSuchMethodError can occur in case application code is partially compiled, or in case an external dependency in a project incompatibly changed the code (e.g. removed the calling method) from one version to another. It is as shown in the illustration below as follows:
Illustration:
java.lang
Class NoSuchMethodError
    java.lang.Object
        java.lang.Throwable
            java.lang.Error
                java.lang.LinkageError
                    java.lang.IncompatibleClassChangeError
                        java.lang.NoSuchMethodError
Note: All Implemented Interfaces is Serializable interface in Java.
Now let us discuss the causes behind this exception in order to figure out how to resolve the same problem. java.lang. It occurs when a particular method is not found. This method can either be an instance method or a static method. The java.lang.NoSuchMethodError occurs when an application does not find a method at runtime. In most cases, we’re able to catch this error at compile-time. Hence, it’s not a big issue. However, sometimes it could be thrown at runtime, then finding it becomes a bit difficult. According to the Oracle documentation, this error may occur at runtime if a class has been incomparably changed. Hence, we may encounter this error in the following cases. Firstly, if we do just a partial recompilation of our code. Secondly, if there is version incompatibility with the dependencies in our application, such as the external jars.
Note: NoSuchMethodError inheritance tree includes IncompatibleClassChangeError and LinkageError. These errors are associated with an incompatible class change after compilation.
Implementation:
Now we will be proposing two examples in which first we will illustrate the thrown exception and, in later example, resolve the same via clean java problems.
Example 1
Java
// Java Program to Demonstrate NoSuchMethodError by // throwing it due to a breaking change // introduced within an application  // Importingn I/O classesimport java.io.*;  // Class 1// Helper classclass NoSuchMethodError {      // Method 1    // Void demo method created to be called    // in another class containing main() method    public void printer(String myString)    {          // Print statement        System.out.println(myString);    }}  // Class 2// Main classpublic class GFG {      // Main driver method    public static void main(String[] args)    {          // Creating object of class 1        NoSuchMethodError obj = new NoSuchMethodError();          // Now calling print() method which is not present        // in NoSuchMethodErrorExample class, hence throwing        // exception        obj.print("Hello World");    }}
Output:
Now if we try to draw out conclusions about the possible solution to resolve the above error. For that, we need to take care of two parameters as listed: 
Call correct method which is present in class.
Check the name of the method and its signature which you are trying to call.
Example 2
Java
// Java Program to Resolve NoSuchMethodError  // Importing input output classesimport java.io.*;  // Class 1// Helper classclass NoSuchMethodError {    // Defined printer method    public void printer(String myString)    {        // Print the string which will be passed        // in the main() method        System.out.println(myString);    }}  // Class 2// Main Classpublic class GFG {      // Main driver method    public static void main(String[] args)    {        // Creating object of above class in        // main() method of this class        NoSuchMethodError obj            = new NoSuchMethodError();          // Calling printer() method which is  present in        // NoSuchMethodErrorExample class        obj.printer("Hello World");    }}
Hello World

Java-Exception Handling
Picked
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.