NoSuchMethodError

java.lang.NoSuchMethodError

一般3种情况下会发生该异常

  1. 一个应用程序调用了一个类(类或者类的实例)中的方法,但是该类中已经不存在该方法的定义,这种情况在编译时就能够发现。
  2. jar 包冲突,导致类版本不一致
  3. A 类 调用 B类的方法,B类中也有该方法,但是B中的方法的返回值调整了(如 int 改为 Integer),B类所在的jar包deploy了,但是A类所在的jar未deploy,导致该错误

先看下类图

先看下源码

package java.lang;

/**
 * Thrown if an application tries to call a specified method of a
 * class (either static or instance), and that class no longer has a
 * definition of that method.
 * <p>
 * Normally, this error is caught by the compiler; this error can
 * only occur at run time if the definition of a class has
 * incompatibly changed.
 *
 * @author  unascribed
 * @since   JDK1.0
 */
public
class NoSuchMethodError extends IncompatibleClassChangeError {
    private static final long serialVersionUID = -3765521442372831335L;

    /**
     * Constructs a <code>NoSuchMethodError</code> with no detail message.
     */
    public NoSuchMethodError() {
        super();
    }

    /**
     * Constructs a <code>NoSuchMethodError</code> with the
     * specified detail message.
     *
     * @param   s   the detail message.
     */
    public NoSuchMethodError(String s) {
        super(s);
    }
}

对源码的注释做个简单的翻译:

如果一个应用程序调用了一个类(类或者类的实例)中的方法,但是该类中已经不存在该方法的定义,则会抛出该异常。
通常情况下,这个错误会在编译期被发现。当且仅当类的定义发生了不兼容的改变时,会发生在运行期。
posted @ 2022-10-13 11:34  刺客一个  Views(156)  Comments(0Edit  收藏  举报