Java Lang NoSuchMethodError

Overview
The Java lang NoSuchMethodError is an error that is thrown at runtime. This error occurs if the compiler does not find a method that is being called. It is a Java error that is caused when a method that exists at compile time, is absent at the run-time. In simple words, a method that exists at compile time but is absent during the run time is called by the user. It shows the method that we are calling does not exist at the runtime which causes the Java lang NoSuchMethodError. The method can either be an instance method or a static method.
Introduction to Java Class NoSuchMethodError
As programmers, we often deal with classes and their methods. We write a class and a specific method inside the class. In the main function, we create an object of the class and call the method on that object. But, there might be a need to remove that function later from your program. In such a case, when you remove the method but still call that method in your main function, an error gets thrown. This error as you guessed it correctly is the Java lang NoSuchMethod error.
The Java.Lang.NoSuchMethodError is a runtime error that gets thrown when the program tries to call a method that does not exist in the class. Mostly it is possible to find this error at the compile time, but it is also thrown at the runtime. So, a method that exists at compile time but is absent during the run time is called by the user resulting in this error. It shows the method that we are calling does not exist at the runtime which causes the Java lang NoSuchMethodError. The method can either be an instance method or a static method.
The Java lang NoSuchMethodError can be thrown due to various reasons the code is partially compiled or the definition of a class has incompatibly changed(that means earlier the class definition was different, but now it is changed by the user). Various reasons will be discussed in depth in the later section.
Illustration
The following is the illustration of the inheritance tree of the NoSuchMethod error in Java.
So, the Java.lang.NoSuchMethodError inheritance tree includes the IncompatibleClassChangeError and LinkageError. The above errors are associated with incompatible class changes after compilation.
All Implemented Interfaces
All implemented interfaces are the Serializable interface in Java. It is present in the java.io package. The Serializable interface is a marker interface. A Marker Interface does not have any methods and fields. Thus classes that are implementing it do not have to implement any methods. Classes implement the Serializable interface if they want their instances to be Serialized or Deserialized.
Constructors Summary of Java Class NoSuchMethodError
The following table shows the various constructors associated with the NoSuchMethodError in Java.
| Sr. No | Constructor | Description |
|---|---|---|
| 1. | NoSuchMethodError() | This method is used to construct a NoSuchMethodError with no detailed message. |
| 2. | NoSuchMethodError(String s) | This method is used to construct a NoSuchMethodError with a detailed message. |
Method Summary of Java Class NoSuchMethodError
The Java class NoSuchMethodError contains methods inherited from class java.lang.Throwable and java.lang.Object. We will discuss some methods inherited from each class in detail.
Some Methods Inherited from Class java.lang.Throwable
| Sr. No | Method | Description |
|---|---|---|
| 1. | final void addSuppressed(Throwable exception) | This method is used to append the specified exception which is passed as the parameter to the exceptions that were suppressed so that this expression could be delivered. |
| 2. | Throwable getCause() | This method is used to return the cause of this error or null if the cause is unknown. |
| 3. | Throwable fillInStackTrace() | This method is used to fill in the execution stack trace. |
| 4. | String getMessage() | This method is used to return the detailed message string of this throwable. |
| 5. | String getLocalizedMessage() | This method is used to create a localized description of this throwable. |
| 6. | void printStackTrace(PrintStream s) | This method is used to return this throwable and its backtrace to the mentioned print stream. |
| 7. | void setStackTrace(StackTraceElement[] stackTrace) | This method is used to set the stack trace elements returned by the getStackTrace() method and which is printed by the printStackTrace() and similar methods. |
| 8. | String toString() | It is used to return the description of this throwable. |
Some Methods Inherited From Class java.lang.Object
| Sr. No | Method | Description |
|---|---|---|
| 1. | toString() | This method is used to return a string representation of an object. It is used to convert an Object "to a String". |
| 2. | hashCode() | This method is used to return the hash code value |
| 3. | equals (Object obj) | This method compares two objects and returns whether they are equal or not. It is used to compare the value of the object on which the method is called and the object value which is passed as the parameter. |
| 4. | getClass() | This method is used to return the class object of this object. Also, it fetches the actual runtime class of the object on which the method is called. |
| 5. | clone() | The clone() method is used to create an exact copy of this object. It creates a new object and copies all the data of this object to the new object. |
| 6. | notify() | The notify() method is used to wake up only one single thread that is waiting on the object, and that thread starts the execution. |
| 7. | notifyAll() | The notifyAll() method is used to wake up all threads that are waiting on this object. This method gives the notification to all waiting threads of an object. |
| 8. | wait() | The wait() method tells the current thread to give up the lock and go to sleep. |
Constructor Detail
- NoSuchMethodError()- This method is used to construct a NoSuchMethodError with no detailed message.
- NoSuchMethodError(String s)- This method is used to construct a NoSuchMethodError with a detailed message. Here, 's' is the detailed message.
What Causes Java Class NoSuchMethodError
When Java Class NoSuchMethodError is thrown, it is because the program does not find the method at runtime. If our code is recompiled partially, we may get this error. Other causes are-
- Breaking change in third party library-
When our program calls a method in a third-party library, which was there at compile time but not at runtime, it results in the java.lang.NoSuchMethodError. It means the third-party library must have removed the method that is being called.
The above indicates an issue with the build as the method existed at the compile time but not at the runtime. There is a possibility that the version of the library that was used during the build might be different than the program version.
- Overriding third-party library version-
A third-party library when used in the program but indirectly can cause the Java lang NoSuchMethodError. For an instance, the version could be a dependency of other third-party libraries which use a different version of that library. This can be solved using tools like Maven or Apache which prevent version conflicts.
How to Solve NoSuchMethodError
The steps to follow to fix the Java.lang.NoSuchMethodError is as follows-
- To do a full clean and compile-
In order to solve the NoSuchMethodError is to do a full clean and then re-compile the projects that contain the called and calling classes. When this is done, it is ensured the latest version of the classes is used.
- Fix the third-party library version issues
If the error arises due to calling a third-party library method, we can find out which library contains the called class and method to solve the issue that persisted.
The user can obtain information about libraries to load the class by using -verbose:class.
Java Class NoSuchMethodError Examples
Example 1
In this program, we will demonstrate the example of a breaking change. We declare two classes, the first class is the helper class that contains a method. Now we will declare our Main class and create the object of the helper class in the Main class. Now we will call a method on the created object which is not present in the helper class. This will lead to NoSuchMethodError.
Output-
As you can see in the above example, we have created a class NoSuchMethodError and written a function inside it. Now we will write the main function and create an object of this class in it. Now we will call a method on the created object which is not present in the helper class. This will lead to NoSuchMethodError as visible in the output.
Example 2
Now we will rectify the above code and fix the above error.
Output-
In the above code, we have followed the same procedure as we have done in the previous example. The only change is we are calling the method which is present in the class definition. This will solve the NoSuchMethodError and yield the correct output.
NoSuchMethodException vs NoSuchMethodError
We are already aware of NoSuchMethodError that which occurs when a compiled Java class does a regular method call to another class and the method doesn't exist. NoSuchMethodException can be thrown when you're invoking a method through reflection, and the name of the method comes from a variable in your program.
Conclusion
- The Java lang NoSuchMethodError is an error that is thrown at the runtime. This error occurs if the compiler does not find a method that is being called. It is a Java error that is caused when a method that exists at compile time, is absent at the run-time.
- The Java lang NoSuchMethodError can be thrown due to various reasons like the code is partially compiled or the definition of a class has incompatibly changed.
- All implemented interfaces are the Serializable interface in Java. It is present in the java.io package.
- In order to solve the NoSuchMethodError is to do a full clean and then re-compile the projects that contain the called and calling classes. We can also fix issues with the third-party library versions.