Home > Java > Java Tutorial > body text

Causes and solutions to the NoSuchMethodError exception in Java

PHPz
Release: 2023-06-24 22:19:33
Original
3777 people have browsed it

Causes and solutions to NoSuchMethodError exceptions in Java

Java is one of the most popular programming languages ​​in the world. It has the characteristics of cross-platform, object-oriented, and security. In Java program development, various exceptions are often encountered. Among them, the NoSuchMethodError exception is also one of the very common exceptions. This article will introduce the causes and solutions of NoSuchMethodError exceptions.

1. Cause

In Java program development, if a non-existent method is called, a NoSuchMethodError exception will occur. There are the following two situations:

1. The method exists at compile time but cannot be found at runtime

When compiling Java code, the Java compiler will check all methods of the class. If a certain If the method does not exist or the signature does not match, compilation will fail. But at run time, when the code calls a method that does not exist, a NoSuchMethodError exception is generated.

For example, the following code will generate a NoSuchMethodError exception:

public class Test {
    public static void main(String[] args) {
        int sum = add(1, 2, 3);
        System.out.println(sum);
    }

    public static int add(int a, int b) {
        return a + b;
    }
}
Copy after login

In this code, we call the add method in the main method and pass in three parameters, but the add method Only two parameters can be accepted, so compilation will fail. But if we pass in two parameters instead, the compilation will pass. But at runtime, because the add method we called passed in three parameters, a NoSuchMethodError exception occurred.

2. There is a method at runtime, but it is not called.

In a Java program, if a class has been compiled into a .class file but is not used at runtime, the JVM will not This class will be loaded, and the methods in this class will not be called. However, if you need to use a method in this class later and this method has been modified, a NoSuchMethodError exception will be generated.

For example, here are two versions of the class:

public class Test {
    public static void main(String[] args) {
        A a = new A();
        a.sayHello();
    }
}

class A {
    public void sayHello() {
        System.out.println("Hello, World!");
    }
}
Copy after login

Another version:

public class Test {
    public static void main(String[] args) {
        A a = new A();
        a.sayHello("Java");
    }
}

class A {
    public void sayHello(String name) {
        System.out.println("Hello, " + name + "!");
    }
}
Copy after login

If we run the first version of the class first and then the second version of the class, a NoSuchMethodError exception will occur because the sayHello method in the second version of the class has changed, but the JVM did not load this new version of the class, but loaded the old version of the class, so when calling The new version of the sayHello method will generate a NoSuchMethodError exception.

2. Solution

The reasons for generating NoSuchMethodError exception are more complicated, so we need to adopt different solutions.

1. The method exists at compile time but cannot be found at runtime

If a method exists at compile time but cannot be found at runtime, you can troubleshoot the problem from the following aspects:

(1) Confirm whether the method exists

Make sure that the method you are calling actually exists. You can use reflection to confirm whether the method exists, for example:

public class Test {
    public static void main(String[] args) throws Exception {
        Class cls = Class.forName("com.example.A");
        Method method = cls.getMethod("add", int.class, int.class);
        System.out.println(method);
    }
}

class A {
    public int add(int a, int b) {
        return a + b;
    }
}
Copy after login

In this code, we use reflection to obtain the add method in class A. If the add method cannot be found at runtime, an exception will be generated.

(2) Check whether the signature of the method matches

The method signature is a unique sequence of characters defined for the method, which usually includes information such as the method name, parameter type, and return type. If the method signature at compile time is inconsistent with that at run time, a NoSuchMethodError exception will be generated.

(3) Confirm whether the class where the method is located has been loaded correctly

Make sure that the class where the method is located has been loaded correctly. You can confirm whether the class is loaded by printing the fully qualified name of the class, for example:

public class Test {
    public static void main(String[] args) {
        A a = new A();
        System.out.println(a.getClass().getName());
    }
}

class A {
    public void sayHello() {
        System.out.println("Hello, World!");
    }
}
Copy after login

In this code, we print the fully qualified name of the class where the a object is located to confirm whether the class is loaded. Loaded correctly.

(4) Confirm whether the class where the method is located has been correctly imported

Make sure the class where the method is located has been correctly imported. If you copy code from elsewhere but do not import the corresponding class correctly, a NoSuchMethodError exception will occur.

2. A method exists at runtime, but is not called.

If a method exists at runtime, but is not called, you can solve the problem through the following methods:

(1) Clear the cache

Ensure that the JVM correctly loads the new version of the class by clearing the cache. In Windows systems, you can enter the following command in cmd to clear Java's cache:

javaws -clearcache
Copy after login

(2) Recompile

Recompile the code and run it again.

(3) Use the new version of the class

Make sure to use the new version of the class instead of the old version of the class.

(4) Modify the name of the class

If you need to modify the name of a class, it is recommended to modify all methods in the class at the same time to ensure that no NoSuchMethodError will occur in subclasses that inherit the class abnormal.

3. Summary

NoSuchMethodError exception is a common problem in Java program development. The reasons for this problem include methods that exist at compile time but cannot be found at runtime, methods that exist at runtime but are not called, etc. Methods to solve this problem mainly include confirming whether the method exists, checking whether the method signature matches, confirming whether the class where the method is located is correctly loaded, confirming whether the class where the method is located is correctly imported, etc. When developing Java programs, you need to pay attention to the difference between compilation and runtime, and choose an appropriate solution according to the specific situation.

The above is the detailed content of Causes and solutions to the NoSuchMethodError exception in Java. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!