我如何修复诺舒德罗?
在运行我的Java程序时,我会收到nosuchmethoderror
错误。怎么了,我该如何解决?
I'm getting a NoSuchMethodError
error when running my Java program. What's wrong and how do I fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
没有更多信息,很难查明问题,但是根本原因是您很可能已经针对缺少方法的类别的类别的类别编译了一个类,而不是您在运行它时使用的方法。
查看堆栈跟踪...如果在库中调用对象上的方法时出现异常,则在编译和运行时,您很可能会使用库的单独版本。确保您都有正确的版本。
如果在通过类实例化的对象上调用方法时出现异常,则您的构建过程似乎是错误的。编译时,请确保您实际运行的类文件会更新。
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 you made, then your build process seems to be faulty. Make sure the class files that you are actually running are updated when you compile.
我遇到了您的问题,这就是我解决的问题。以下步骤是添加库的一种工作方式。我已经正确完成了前两个步骤,但是我没有通过将“ .jar”文件直接从文件系统拖动到我的Eclipse项目上的“ lib”文件夹中完成了最后一个步骤。此外,我必须从构建路径和“ lib”文件夹中删除库的先前版本。
步骤1-添加.jar到构建路径
步骤2-关联源和Javadocs(可选)
步骤3-实际上将.jar文件拖动到“ lib”文件夹(不是可选)
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.
Step 1 - Add .jar to build path
Step 2 - Associate sources and javadocs (optional)
Step 3 - Actually drag .jar file into "lib" folder (not optional)
请注意,在反思的情况下,您会得到
nosuchmethodexception
,而对于非反射代码,您可以获得nosuchmethoderror
。当面对一个与另一个相对于另一个时,我倾向于在非常不同的地方看。Note that in the case of reflection, you get an
NoSuchMethodException
, while with non-reflective code, you getNoSuchMethodError
. I tend to go looking in very different places when confronted with one versus the other.如果您可以访问更改JVM参数,则添加详细输出应使您可以查看正在加载哪些JAR文件的类。
运行程序时,JVM应倾倒以标准信息,例如:
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.
When your program is run, the JVM should dump to standard out information such as:
如果使用 maven 或其他框架,并且几乎可以随机遇到此错误,请尝试使用干净的安装...
如果您编写对象并且您知道它具有该方法,则尤其有可能工作。
If using Maven or another framework, and you get this error almost randomly, try a clean install like...
This is especially likely to work if you wrote the object and you know it has the method.
这通常是在使用
有时,这也可能是针对一个版本的库时,但要与其他版本进行竞争时会引起。
This is usually caused when using a build system like Apache Ant that 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").
Sometimes this can also be caused when compiling against one version of a library but running against a different version.
我有同样的错误:
要解决它,我首先检查了, (
单击您的POM组合 - > ctrl+alt+shift+shift+u
或右键单击您的pom - > maven-maven-> show依赖关系
)了解图书馆之间的冲突到底在哪里(Intelij Idea)。在我的特殊情况下,我有不同版本的杰克逊依赖性。1)因此,我直接在项目的POM中明确添加了这两个版本的最高版本-2.8.7。
在属性中:
和作为依赖性:
2),但也可以使用依赖项排除。
按照与以下相同的原则:
依赖性版本的依赖性将被排除在您的项目之外。
I had the same error:
To solve it I checked, firstly, Module Dependency Diagram (
click in your POM the combination -> Ctrl+Alt+Shift+U
orright click in your POM -> Maven -> Show dependencies
) to understand where exactly was the conflict between libraries (Intelij IDEA). In my particular case, I had different versions of Jackson dependencies.1) So, I added directly in my POM of the project explicitly the highest version - 2.8.7 of these two.
In properties:
And as dependency:
2) But also it can be solved using Dependency Exclusions.
By the same principle as below in example:
Dependency with unwanted version will be excluded from your project.
为什么有人不提依赖性冲突?这个常见问题可能与包含不同版本的依赖罐有关。
详细的说明和解决方案: https://dzone.com/articles/solvice.com/articles/solving-dependence-dependency-dependency--------冲突中的冲突
简短答案;
添加此Maven依赖性;
然后运行此命令;
也许这是您面临的问题。
Why anybody doesn't mention dependency conflicts? This common problem can be related to included dependency jars with different versions.
Detailed explanation and solution: https://dzone.com/articles/solving-dependency-conflicts-in-maven
Short answer;
Add this maven dependency;
Then run this command;
Maybe this is the cause your the issue you faced.
如果您正在撰写WebApp,请确保您的集装箱全局库目录中的JAR和应用程序中都没有相互矛盾的版本。您可能不一定知道Class Loader正在使用哪个JAR。
例如
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.
e.g.
对我来说,这是因为我更改了函数中的参数类型,从对象a,字符串a。我可以用干净并再次建造解决
For me it happened because I changed argument type in function, from Object a, to String a. I could resolve it with clean and build again
就我而言,我有一个多模块项目,场景就像
com.xyz.testclass
在模块a
以及模块b
中。模块a
取决于模块b
。因此,当创建一个汇编罐时,我认为如果没有调用方法,我认为只保留了一个类的类别,那么我得到了nosuchmethoderror
runtime>运行时异常,但是汇编很好。相关: https://reflectoring.io/nosuchmethod/
In my case I had a multi module project and scenario was like
com.xyz.TestClass
was in moduleA
and as well as in moduleB
and moduleA
was dependent on moduleB
. So while creating a assembly jar I think only one version of class was retained if that doesn't have the invoked method then I was gettingNoSuchMethodError
runtime exception, but compilation was fine.Related : https://reflectoring.io/nosuchmethod/
这也可能是使用反射的结果。如果您的代码反映了类并按名称提取方法(例如:with
class.getDeclaredMethod(“ somemethodname”,.....)
),那么任何时间都会更改该方法名称,例如就像在重构期间一样,您需要记住将参数更新为反射方法以匹配新方法签名,或者getDeclaredMethod
呼叫将抛出nosuchmethodexception
。如果这是原因,则堆栈跟踪应显示调用反射方法的观点,您只需要更新参数即可匹配实际方法签名。
以我的经验,当单元测试私人方法/字段以及使用
Testutilitys
类提取字段以进行测试验证时,这偶尔会出现。 (通常使用未考虑单位测试设计的旧代码。)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 thegetDeclaredMethod
call will throw aNoSuchMethodException
.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.)这意味着同类中不存在各自的方法:
It means the respective method is not present in the class:
我刚刚通过重新启动日食并运行应用程序来解决此错误。
我的案件的原因可能是因为我在不关闭项目或Eclipse的情况下更换源文件。
这引起了我正在使用的不同类别的类别。
I have just solved this error by restarting my Eclipse and run the applcation.
The reason for my case may because I replace my source files without closing my project or Eclipse.
Which caused different version of classes I was using.
尝试这样做:删除项目目录下的所有.class文件(当然,所有子目录)。重建。
有时,
MVN Clean
(如果使用Maven)不会清洁。.class文件由javac
手动创建。这些旧文件包含旧签名,导致nosuchmethoderror
。Try this way: remove all .class files under your project directories (and, of course, all subdirectories). Rebuild.
Sometimes
mvn clean
(if you are using maven) does not clean .class files manually created byjavac
. And those old files contain old signatures, leading toNoSuchMethodError
.只是增加了现有答案。我在Eclipse中与Tomcat面对这个问题。我已经更改了一个课程,并做了以下步骤,
在Eclpise
中清理并构建
mvn Clean selplins intastion
我仍然面临相同的错误。然后,我清洁了tomcat,清洁的tomcat工作目录和重新启动的服务器,我的问题已经消失了。希望这对某人有帮助
Just adding to existing answers. I was facing this issue with tomcat in eclipse. I had changed one class and did following steps,
Cleaned and built the project in eclpise
mvn clean install
Still I was facing same error. Then I cleaned tomcat, cleaned tomcat working directory and restarted server and my issue is gone. Hope this helps someone
这些问题是由于在同一两个类中使用相同对象引起的。
所使用的对象不包含新方法,即新对象类包含。
例如:
这些问题是由伴随02类似类引起的(src中的1,jar文件中的1个是gateway.jar)
These problems are caused by the use of the same object at the same two classes.
Objects used does not contain new method has been added that the new object class contains.
ex:
These problems are caused by the concomitant 02 similar class (1 in src, 1 in jar file here is gateway.jar)
回答原始问题。根据java文档
通常,此错误被编译器捕获。只有当类的定义不兼容时,此错误才能在运行时间发生。
To answer the original question. According to java docs here:
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.
我通过重命名Junit测试文件解决了Eclipse的问题。
在我的日食工作空间中,我有一个应用程序项目和一个测试项目。
测试项目将应用程序项目作为构建路径上的必需项目。
开始获得Nosuchmethoderror。
然后,我意识到测试项目中的课程与应用程序项目中的类具有相同的名称。
将测试重命名为正确的名称“ PropentTest.java”之后,例外消失了。
I fixed this problem in Eclipse by renaming a Junit test file.
In my Eclipse work space I have an App project and a Test project.
The Test project has the App project as a required project on the build path.
Started getting the NoSuchMethodError.
Then I realized the class in the Test project had the same name as the class in the App project.
After renaming the Test to the correct name "ProjectionTest.java" the exception went away.
Nosuchmethoderror:我花了几个小时来解决此问题,最后通过重命名包装名称,清洁和构建来修复它。它应该修复。祝你好运。
NoSuchMethodError : I have spend couple of hours fixing this issue, finally fixed it by just renaming package name , clean and build ... Try clean build first if it doesn't works try renaming the class name or package name and clean build...it should be fixed. Good luck.
当我在应用程序中更改方法签名时,我遇到了类似的问题。
清洁和重建我的项目解决了“ Nosuchmethoderror”。
I ran into a similar problem when I was changing method signatures in my application.
Cleaning and rebuilding my project resolved the "NoSuchMethodError".
上面的答案很好地解释了..添加一件事
如果使用Eclipse使用Ctrl+Shift+T,然后输入类的软件包结构(例如:Gateway.smpp.pdueventlistener),您将在目前的所有罐子/项目中找到所有罐子/项目。从类路径中删除不必要的罐子或在类路径中添加。现在,它将选择正确的一个。
Above answer explains very well ..just to add one thing
If you are using using eclipse use ctrl+shift+T and enter package structure of class (e.g. : gateway.smpp.PDUEventListener ), you will find all jars/projects where it's present. Remove unnecessary jars from classpath or add above in class path. Now it will pick up correct one.
我遇到了类似的问题。
最后,我确定了根本原因是更改变量的数据类型。
lightee.java
- >包含其数据类型已从int
更改为string
的变量(empid
)。reportgeneration.java
- >使用getter,getempid()
检索值。我们应该仅包括修改后的类,来重新启动罐子。由于
reportgeneration.java
i仅在JAR文件中包含employee.class
。我必须在JAR中包括reportgeneration.class
文件才能解决问题。I ran into similar issue.
Finally I identified the root cause was changing the data type of variable.
Employee.java
--> Contains the variable (EmpId
) whose Data Type has been changed fromint
toString
.ReportGeneration.java
--> Retrieves the value using the getter,getEmpId()
.We are supposed to rebundle the jar by including only the modified classes. As there was no change in
ReportGeneration.java
I was only including theEmployee.class
in Jar file. I had to include theReportGeneration.class
file in the jar to solve the issue.我遇到了同样的问题。这也是当课程含糊不清时引起的。我的程序试图调用一种在同一位置 /类路径中存在的两个JAR文件中存在的方法。删除一个JAR文件或执行您的代码,以便仅使用一个JAR文件。检查您是否使用相同的JAR或不同版本的同一JAR包含同一类的罐子。
I've had the same problem. This is also caused when there is an ambiguity in classes. My program was trying to invoke a method which was present in two JAR files present in the same location / class path. Delete one JAR file or execute your code such that only one JAR file is used. Check that you are not using same JAR or different versions of the same JAR that contain the same class.
最佳解释: https://wwww.journaldev.com/wwwww.journaldev.com/14538/14538/java-java-java-nang-nang-nosung-nosuchmetermetermetermetermeterorrerrorrer
Best Explanation: https://www.journaldev.com/14538/java-lang-nosuchmethoderror
我也遇到了这个错误。
我的问题是,我更改了方法的签名,类似的
方法
是从类似于
编译器的环境中调用的,这在警告/错误方面是沉默的,因为资本既是货币又是欧元。
出现问题是因为我仅编译了定义该方法的类 - 银行,而不是从中调用该方法的类,其中包含main()方法。
这个问题不是您可能经常遇到的问题,因为大多数项目经常进行人工重建或自动触发构建操作,而不仅仅是编译一个修改的类。
我的用户酶是我生成了一个.jar文件,该文件将用作热五个文件,该文件不包含App.Class,因为它没有被修改。当我保留最初的论点的基类槽继承时,我不包括它是有意义的。
事实是,当您编译类时,结果字节码是 static ,换句话说,这是 hard-reference 。
原始的拆卸字节码(由Javap工具生成)看起来像这样:
ClassLoader加载新的编译银行。Class.Class。它找不到这样的方法,看起来好像是被删除并没有更改,因此命名错误。
希望这会有所帮助。
I've encountered this error too.
My problem was that I've changed a method's signature, something like
into
This method was invoked from a context similar to
The compiler was silent with regard to warnings/ errors, as capital is both Currency as well as Euro.
The problem appeared due to the fact that I only compiled the class in which the method was defined - Bank, but not the class from which the method is being called from, which contains the main() method.
This issue is not something you might encounter too often, as most frequently the project is rebuilt mannually or a Build action is triggered automatically, instead of just compiling the one modified class.
My usecase was that I generated a .jar file which was to be used as a hotfix, that did not contain the App.class as this was not modified. It made sense to me not to include it as I kept the initial argument's base class trough inheritance.
The thing is, when you compile a class, the resulting bytecode is kind of static, in other words, it's a hard-reference.
The original disassembled bytecode (generated with the javap tool) looks like this:
After the ClassLoader loads the new compiled Bank.class, it will not find such a method, it appears as if it was removed and not changed, thus the named error.
Hope this helps.
在我的情况下,问题是在构建路径中有两个版本的同一库。库的较旧版本没有功能,而较新的版本则没有。
The problem in my case was having two versions of the same library in the build path. The older version of the library didn't have the function, and newer one did.
我使用Intelij的Gradle项目也有类似的问题。
我通过删除.gradle(请参阅下面的屏幕截图)包裹并重建项目来解决它。
。gradle package
I had a similar problem with my Gradle Project using Intelij.
I solved it by deleting the .gradle (see screenshot below) Package and rebuilding the Project.
.gradle Package
我遇到了同样的问题。我更改了一种方法的返回类型,并运行了该类别的测试代码。那是我面对这个
nosuchmethoderror
的时候。作为解决方案,在再次运行测试代码之前,我在整个存储库上运行了Maven。在下一个单一测试运行中解决了问题。I had faced the same issue. I changed the return type of one method and ran the test code of that one class. That is when I faced this
NoSuchMethodError
. As a solution, I ran the maven builds on the entire repository once, before running the test code again. The issue got resolved in the next single test run.在使用Quarkus时,我通过删除目标折线并重新启动程序解决了问题。
I solved the problem when using Quarkus by removing the target-folder and restart the program.