如何获取java.lang.NoSuchMethodError [英] How to get java.lang.NoSuchMethodError

查看:56
本文介绍了如何获取java.lang.NoSuchMethodError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

还有很多问题,例如如何在SO上修复java.lang.NoSuchMethodError .

如我所见,发生此错误的最简单方法是制作一个类

As I can see, the simplest way to get this error, is to make a class

class MyClass {} // no methods at all, for instance

没有正确定义的主方法,编译并运行:

without properly defined main method, compile it and run:

java MyClass

出现异常:

Exception in thread "main" java.lang.NoSuchMethodError: main

但是这个例子太简单了.

But this example is too simple.

任何人都可以提供一个简单的代码,

Could anyone provide a simple code, which

  1. 由两个(最多三个)类组成(如果您可以证明需要更多的类,那么欢迎您);
  2. 包含正确定义的主方法;
  3. 使用该主要方法来运行类,会导致 java.lang.NoSuchMethodError 出现异常.
  1. Consists of two, maximum three classes (if you can show that more classes are needed - then you are welcome);
  2. Contains properly defined main method;
  3. Running of the class with that main method, leads to exception with java.lang.NoSuchMethodError.

推荐答案

NoSuchMethodError ,但是在运行时另一个类没有该方法.因此,您需要:

NoSuchMethodError happens if one class expects a method in another class (and was compiled with that method in place), but at runtime the other class does not have that method. So you need to:

  • 创建两个类,其中一个在另一个上调用方法
  • 编译两个类
  • 然后从第二个类中删除被调用的方法,并仅编译第二个类

然后,如果您运行第一个类(使用main方法),则尝试在第二个类上调用该方法(该方法不再存在)时将抛出该错误

Then, if you run the first class (with main method), it will throw that error when trying to call the method on the 2nd class (the method no longer exists)

尽管这个例子在现实世界中很少发生.这是发生错误的一些实际情况:

This example would rarely happen in the real world though. Here are some real cases when the error occurs:

  • 您正在使用依赖于另一个jar的第3方库(jar).但是,您使用的这些jar版本不兼容,第一种会尝试在第二种中的类上调用方法不再存在的jar.
  • 您的编译时和运行时类路径不同-您已针对某些库的版本(也可以是JDK本身)编译代码,但您的运行时具有其他版本
  • 您有一个多模块项目.您的IDE在编译时链接"项目,因此可以立即看到任何更改.但是在构建时,您忘了编译其中一个模块(在其中添加了方法),因此在运行时它正在使用旧版本.

这篇关于如何获取java.lang.NoSuchMethodError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆