Java 如何修复 NoSuchMethodError?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I fix a NoSuchMethodError?
提问by John Meagher
I'm getting a NoSuchMethodError
error when running my Java program. What's wrong and how do I fix it?
NoSuchMethodError
运行 Java 程序时出现错误。出了什么问题,我该如何解决?
采纳答案by Vetle
Without any more information it is difficult to pinpoint the problem, but the root cause is that you most likely have compiled a class against a different version of the class that is missing a method, than the one you are using when running it.
如果没有更多信息,就很难查明问题,但根本原因是您很可能已经针对缺少方法的类的不同版本编译了一个类,而不是您在运行它时使用的那个版本。
Look at the stack trace ... If the exception appears when calling a method on an object in a library, you are most likely using separate versions of the library when compiling and running. Make sure you have the right version both places.
查看堆栈跟踪...如果在对库中的对象调用方法时出现异常,则您很可能在编译和运行时使用了库的不同版本。确保您在两个地方都有正确的版本。
If the exception appears when calling a method on objects instantiated by classes youmade, then your build process seems to be faulty. Make sure the class files that you are actually running are updated when you compile.
如果在您创建的类实例化的对象上调用方法时出现异常,那么您的构建过程似乎有问题。确保在编译时更新了您实际运行的类文件。
回答by John Meagher
This is usually caused when using a build system like Apache Antthat only compiles java files when the java file is newer than the class file. If a method signature changes and classes were using the old version things may not be compiled correctly. The usual fix is to do a full rebuild (usually "ant clean" then "ant").
这通常是在使用像Apache Ant这样的构建系统时引起的,该系统仅在 java 文件比类文件更新时才编译 java 文件。如果方法签名更改并且类使用旧版本,则可能无法正确编译。通常的解决方法是进行完全重建(通常是“ant clean”然后是“ant”)。
Sometimes this can also be caused when compiling against one version of a library but running against a different version.
有时这也可能是在针对一个库版本进行编译但针对不同版本运行时引起的。
回答by rcreswick
This can also be the result of using reflection. If you have code that reflects on a class and extracts a method by name (eg: with Class.getDeclaredMethod("someMethodName", .....)
) then any time that method name changes, such as during a refactor, you will need to remember to update the parameters to the reflection method to match the new method signature, or the getDeclaredMethod
call will throw a NoSuchMethodException
.
这也可能是使用反射的结果。如果您的代码反映在类上并按名称提取方法(例如: with Class.getDeclaredMethod("someMethodName", .....)
),那么任何时候该方法名称发生更改,例如在重构期间,您都需要记住更新反射方法的参数以匹配新方法签名,否则getDeclaredMethod
调用将抛出一个NoSuchMethodException
.
If this is the reason, then the stack trace should show the point that the reflection method is invoked, and you'll just need to update the parameters to match the actual method signature.
如果这是原因,那么堆栈跟踪应该显示调用反射方法的点,您只需要更新参数以匹配实际方法签名。
In my experience, this comes up occasionally when unit testing private methods/fields, and using a TestUtilities
class to extract fields for test verification. (Generally with legacy code that wasn't designed with unit testing in mind.)
根据我的经验,在对私有方法/字段进行单元测试以及使用TestUtilities
类提取字段进行测试验证时,偶尔会出现这种情况。(通常使用未考虑单元测试的遗留代码。)
回答by erickson
Note that in the case of reflection, you get an NoSuchMethodException
, while with non-reflective code, you get NoSuchMethodError
. I tend to go looking in very different places when confronted with one versus the other.
请注意,在反射的情况下,您会得到NoSuchMethodException
,而对于非反射代码,您会得到NoSuchMethodError
。当面对一个和另一个时,我倾向于去非常不同的地方寻找。
回答by Cheekysoft
If you are writing a webapp, ensure that you don't have conflicting versions of a jar in your container's global library directory and also in your app. You may not necessarily know which jar is being used by the classloader.
如果您正在编写 web 应用程序,请确保容器的全局库目录和应用程序中没有冲突的 jar 版本。您可能不一定知道类加载器正在使用哪个 jar。
e.g.
例如
- tomcat/common/lib
- mywebapp/WEB-INF/lib
- tomcat/common/lib
- mywebapp/WEB-INF/lib
回答by matt b
If you have access to change the JVM parameters, adding verbose output should allow you to see what classes are being loaded from which JAR files.
如果您有权更改 JVM 参数,添加详细输出应该允许您查看从哪些 JAR 文件加载哪些类。
java -verbose:class <other args>
When your program is run, the JVM should dump to standard out information such as:
当您的程序运行时,JVM 应该转储到标准输出信息,例如:
...
[Loaded junit.framework.Assert from file:/C:/Program%20Files/junit3.8.2/junit.jar]
...
...
[从文件加载 junit.framework.Assert:/C:/Program%20Files/junit3.8.2/junit.jar]
...
回答by Chris Dutrow
I was having your problem, and this is how I fixed it. The following steps are a working way to add a library. I had done the first two steps right, but I hadn't done the last one by dragging the ".jar" file direct from the file system into the "lib" folder on my eclipse project. Additionally, I had to remove the previous version of the library from both the build path and the "lib" folder.
我遇到了你的问题,这就是我解决它的方法。以下步骤是添加库的有效方法。我已经正确地完成了前两个步骤,但是我没有通过将“.jar”文件直接从文件系统拖到我的 eclipse 项目上的“lib”文件夹中来完成最后一个步骤。此外,我必须从构建路径和“lib”文件夹中删除先前版本的库。
Step 1 - Add .jar to build path
步骤 1 - 添加 .jar 以构建路径
Step 2 - Associate sources and javadocs (optional)
第 2 步 - 关联源代码和 javadoc(可选)
Step 3 - Actually drag .jar file into "lib" folder (not optional)
第 3 步 - 实际将 .jar 文件拖入“lib”文件夹(非可选)
回答by Reachgoals
To answer the original question. According to java docs here:
回答原来的问题。根据这里的java文档:
"NoSuchMethodError" 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.
"NoSuchMethodError" 如果应用程序尝试调用类(静态或实例)的指定方法,并且该类不再具有该方法的定义,则抛出该错误。
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.
通常,这个错误是由编译器捕获的;如果类的定义发生了不兼容的更改,则此错误只会在运行时发生。
- If it happens in the run time, check the class containing the method is in class path.
- Check if you have added new version of JAR and the method is compatible.
- 如果在运行时发生,请检查包含该方法的类是否在类路径中。
- 检查您是否添加了新版本的 JAR 并且方法兼容。
回答by knorman
I ran into a similar problem when I was changing method signatures in my application. Cleaning and rebuilding my project resolved the "NoSuchMethodError".
我在更改应用程序中的方法签名时遇到了类似的问题。清理和重建我的项目解决了“NoSuchMethodError”。
回答by Amit Walke
If your file name is different than the class name which contain main method then it may be the possibility that this error may cause.
如果您的文件名与包含 main 方法的类名不同,则可能会导致此错误。