如何解决Java的.lang.NoSuchMethodError在Java中?
顾名思义, Java.lang.NoSuchMethodError是Java的运行时错误,当调用在编译时存在但在运行时不存在的方法时会发生这种错误。 Java.lang.NoSuchMethodError 可能发生在应用程序代码被部分编译的情况下,或者项目中的外部依赖不兼容地将代码从一个版本更改为另一个版本(例如删除了调用方法)。如下图所示:
插图:
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.
现在让我们讨论这个异常背后的原因,以找出如何解决同样的问题。 Java的.lang。它发生在找不到特定方法时。此方法可以是实例方法或静态方法。 Java.lang.NoSuchMethodError 当应用程序在运行时找不到方法时发生。在大多数情况下,我们能够在编译时捕获此错误。因此,这不是一个大问题。但是,有时它可能会在运行时抛出,然后发现它变得有点困难。根据 Oracle 文档,如果类发生了无可比拟的更改,则在运行时可能会发生此错误。因此,我们可能会在以下情况下遇到此错误。首先,如果我们只对我们的代码进行部分重新编译。其次,如果我们的应用程序中的依赖项存在版本不兼容,例如外部jar。
Note: NoSuchMethodError inheritance tree includes IncompatibleClassChangeError and LinkageError. These errors are associated with an incompatible class change after compilation.
执行:
现在我们将提出两个示例,首先我们将说明抛出的异常,然后在后面的示例中,通过干净的Java问题解决相同的问题。
示例 1
Java
// Java Program to Demonstrate NoSuchMethodError by
// throwing it due to a breaking change
// introduced within an application
// Importingn I/O classes
import java.io.*;
// Class 1
// Helper class
class 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 class
public 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");
}
}
Java
// Java Program to Resolve NoSuchMethodError
// Importing input output classes
import java.io.*;
// Class 1
// Helper class
class 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 Class
public 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");
}
}
输出:
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.
示例 2
Java
// Java Program to Resolve NoSuchMethodError
// Importing input output classes
import java.io.*;
// Class 1
// Helper class
class 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 Class
public 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