如何获取 java.lang.NoSuchMethodError
简介:
SO 上有很多问题,如 How to fix java.lang.NoSuchMethodError 。
正如我所看到的,出现此错误的最简单方法是创建一个
class MyClass {} // no methods at all, for instance
没有正确定义的类 主要方法, 编译并运行:
java MyClass
出现异常:
Exception in thread "main" java.lang.NoSuchMethodError: main
但是这个例子太简单了。
问题:
任何人都可以提供一个简单的代码,它
Introduction:
There are many questions as How to fix java.lang.NoSuchMethodError on SO.
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 emerges:
Exception in thread "main" java.lang.NoSuchMethodError: main
But this example is too simple.
The question:
Could anyone provide a simple code, which
- Consists of two, maximum three classes (if you can show that more classes are needed - then you are welcome);
- Contains properly defined main method;
- Running of the class with that main method, leads to exception with java.lang.NoSuchMethodError.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
NoSuchMethodError
发生如果一个类需要另一个类中的方法(并且使用该方法进行编译),但在运行时另一个类没有该方法。因此,您需要:然后,如果您运行第一个类(使用 main 方法) ),当尝试调用第二个类上的方法时(该方法不再存在),它会抛出该错误,
但这个例子在现实世界中很少发生。以下是发生错误时的一些真实情况:
不再存在的 jar。
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: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 that does not exist anymore/yet.
从一个类创建一个类文件,该类在其主方法中调用
java.util.Properties.load(Reader)
,其中 Java 版本 >= 1.6.xxxx。尝试使用某些 Java 版本执行此类1.6.xxxx
原因:
java.util.Properties.load(Reader)
是在 Java 6 中引入的。它正在被调用,但在此版本的 Java 中不存在。这同样适用于更新中引入默认语言库的所有方法。
Create a class file from a class which calls
java.util.Properties.load(Reader)
in its main method with some Java version >= 1.6.xxxx.Attempt to execute this class using some Java version < 1.6.xxxx
Reason:
java.util.Properties.load(Reader)
was introduced in Java 6. It is being called, but does not exist in this version of Java.This applies similarly to all methods introduced to the default language libraries in updates.