Java“NoSuchMethodError”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3695340/
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
Java "NoSuchMethodError"
提问by vrm
I'm getting:
我越来越:
NoSuchMethodError: com.foo.SomeService.doSmth()Z
Am I understanding correctly that this 'Z'
means that return type of doSmth() method is boolean? If true, then that kind of method really does not exist because this method returns some Collection. But on the other hand if I call this method, I'm not assigning its return value to any variable. I just call this method like this:
我是否正确理解这'Z'
意味着 doSmth() 方法的返回类型是布尔值?如果为真,那么这种方法真的不存在,因为这种方法返回一些集合。但另一方面,如果我调用此方法,则不会将其返回值分配给任何变量。我只是这样调用这个方法:
service.doSmth();
Any ideas why this error occurs? All necessary JAR files exist and all other methods from this class seems to exist.
任何想法为什么会发生此错误?所有必需的 JAR 文件都存在,并且该类中的所有其他方法似乎都存在。
采纳答案by amorfis
Looks like method exists in classpath during compilation, but not during running of your application.
看起来方法在编译期间存在于类路径中,但在应用程序运行期间不存在。
I don't think return type is a problem. If it was, it wouldn't compile. Compiler throws error when method call is ambiguous, and it is when two methods differ only by return type.
我不认为返回类型是一个问题。如果是,它就不会编译。当方法调用不明确时编译器会抛出错误,并且当两个方法仅通过返回类型不同时。
回答by Bozho
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.
通常,这个错误是由编译器捕获的;如果类的定义发生了不兼容的更改,则此错误只会在运行时发生。
In short - a class/jar file at runtime is not the same that you used at compile time.
简而言之 - 运行时的类/jar 文件与您在编译时使用的不同。
回答by Vivien Barousse
This is probably a difference between your compile-time classpath and you run-time classpath.
这可能是编译时类路径和运行时类路径之间的差异。
Here is what seems to be going on:
以下是似乎正在发生的事情:
- The code is compiled with a class path that defines the
doSmth()
method returning a boolean. The byte-code refers to thedoSmth()Z
method. - At runtime, the
doSmth()Z
method isn't found. A method returning a Collection is found instead.
- 代码使用定义
doSmth()
返回布尔值的方法的类路径编译。字节码指的是doSmth()Z
方法。 - 在运行时,
doSmth()Z
找不到该方法。而是找到了返回 Collection 的方法。
To correct this problem, check your (compile time) classpath.
要更正此问题,请检查您的(编译时)类路径。
回答by ekangas
I noticed this problem occurring while testing some experimental changes in multiple linked projects, after updating them from SVN in Eclipse.
在 Eclipse 中从 SVN 更新它们后,我注意到在多个链接项目中测试一些实验性更改时发生了这个问题。
Specifically, I updated all projects from SVN, and reverted the .classpath file rather than edit it manually to keep things simple.
具体来说,我从 SVN 更新了所有项目,并恢复了 .classpath 文件,而不是手动编辑它以保持简单。
Then I re-added the linked projects to the path, but forgot to remove the related jars. This was how the problem occurred for me.
然后我将链接的项目重新添加到路径中,但忘记删除相关的jar。这就是我遇到问题的方式。
So apparently the run time used the jar file while the compiler used the project files.
显然,运行时使用了 jar 文件,而编译器使用了项目文件。
回答by Capagris
The current reply just tell you why is failing. Usually is even nicer to know how to fix problems. As it is mentioned, the problem usually is that you built your program but when running or exporting it, the library is not included. So the solution is...
当前的回复只是告诉你为什么失败。通常知道如何解决问题会更好。如前所述,问题通常是您构建了程序,但在运行或导出它时,不包含该库。所以解决办法是...
If you are running, check the the run configuration Select Run tab -> Run configurations -> Select the configuration you are running -> Check the Classpath tab -> Ensure the libraries you need are there
如果您正在运行,请检查运行配置选择运行选项卡 -> 运行配置 -> 选择您正在运行的配置 -> 检查类路径选项卡 -> 确保您需要的库在那里
If you are exporting (for example a war file), follow this Select project -> Select properties -> Select Deployment Assembly -> Press Add -> Select Java Build Path Entries -> Select the libraries you want to be included in your exported file (for example a war file)
如果要导出(例如,war 文件),请按照此选择项目 -> 选择属性 -> 选择部署程序集 -> 按添加 -> 选择 Java 构建路径条目 -> 选择要包含在导出文件中的库(例如War文件)
In both cases, ensure the library which you are referencing in included.
在这两种情况下,请确保包含您要引用的库。
Other frequent problems for this error are not the right type of parameters or visibility but then, the compiler will detect the error before running. In this case, just check the documentation to match the function and package visibility, and ensure that the library is found in Java Build Path in your project properties.
此错误的其他常见问题是参数类型或可见性不正确,但是编译器会在运行之前检测到错误。在这种情况下,只需检查文档以匹配函数和包的可见性,并确保在项目属性的 Java 构建路径中找到该库。
回答by Christian
Another way this can happen and is difficult to find:
另一种可能发生这种情况并且很难找到的方式:
If a signature of a method in an external jar changes in a way that there is no error found in the IDE because it's still compatible with how you call it the class might not be re-compiled.
如果外部 jar 中方法的签名以在 IDE 中未发现错误的方式更改,因为它仍然与您调用它的方式兼容,则该类可能不会重新编译。
If your build checks the files for changes and only then recompiles them, the class might not be recompiled during the build process.
如果您的构建检查文件的更改,然后才重新编译它们,则在构建过程中可能不会重新编译该类。
So when you run it this might lead to that problem. Although you have the new jar, your own code expects still the old one but does never complain.
因此,当您运行它时,这可能会导致该问题。尽管您拥有新的 jar,但您自己的代码仍然期望使用旧的 jar,但从不抱怨。
To make it harder it depends on the jvm if it can handle such cases. So in the worst case it runs on the test server but not on the live machine.
为了使它更难,它取决于 jvm 是否可以处理这种情况。所以在最坏的情况下,它运行在测试服务器上,而不是在现场机器上。
回答by dgisbert
Maybe still can help somebody, but this exception can happen also when you have on the classpath two classes in different jar files that have the same exact signature but they haven't the same public methods.
也许仍然可以帮助某人,但是当您在类路径上有两个具有完全相同签名但它们没有相同公共方法的 jar 文件中的类时,也会发生此异常。
For example:
例如:
On file mylibrary1.jar you have class com.mypackage.mysubpackage.MyClass with method doSmth()
On file mylibrary2.jar you have class com.mypackage.mysubpackage.MyClass withoutmethod doSmth()
在文件 mylibrary1.jar 上你有类 com.mypackage.mysubpackage.MyClass 和方法 doSmth()
在文件 mylibrary2.jar 上你有没有方法 doSmth() 的类 com.mypackage.mysubpackage.MyClass
When searching the class, the classloader may find first mylibrary2.jar depending on the path precedence but can't find the method on that class.
在搜索类时,类加载器可能会根据路径优先级找到第一个 mylibrary2.jar,但找不到该类上的方法。
Be sure you don't have the same package + class on two different files.
确保在两个不同的文件上没有相同的包 + 类。