Java 包、项目、NoSuchMethod 错误
我有两个链接的项目,这两个项目下都有一个包。
X项目-> X包-> X类-> X1方法(); X2方法();
Y项目-> Y包-> Y类-> Y1方法();
我正在尝试从 Y1 方法调用 X1 和 X2 方法。我可以调用 X1 方法,但是当我调用 X2 方法时,出现运行时错误 (java.lang.NoSuchMethodError:)
所有方法都是公共的,并且方法名称没有任何问题。让其中一个工作而另一个却给出运行时错误是无稽之谈。
任何帮助将不胜感激。 谢谢。
I have 2 projects linked and these 2 projects each have a package under them.
XProject -> XPackage -> XClass -> X1Method(); X2Method();
YProject -> YPackage -> YClass -> Y1Method();
I'm trying to call X1 and X2 methods from Y1 Method. I can call X1 Method but when I call X2 method I get a runtime error (java.lang.NoSuchMethodError:)
All methods are public and there is nothing wrong with method names. It is just nonsense to have one of them working while other is giving runtime errors.
Any help would be appreciated.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自
java.lang.NoSuchMethodError
javadoc:换句话说,运行时类路径不包含具有所需方法的类,而它在编译时类路径中可用。总结:你的类路径搞砸了。清理并对齐。如何做到这一点完全取决于您的环境。
更新:因此,它是 Eclipse 中的 Java EE Web 应用程序?假设一个是动态Web项目,另一个是普通Java项目,则在动态Web项目的项目属性中您需要在构建路径和Java EE 模块依赖项中添加正常的Java 项目。
如果这不能解决问题,那么很可能应用程序服务器或 JRE 的默认类路径是脏的。您需要从
JRE/lib
、JRE/lib/ext
和Tomcat/lib
中删除任何不必要的项目相关库,并向自己保证你不再接触那些库路径了;)From the
java.lang.NoSuchMethodError
javadoc:In other words, the runtime classpath doesn't contain the class with the desied method, while it was available in the compiletime classpath. Summarized: your classpath is messed up. Cleanup and align it. How to do it exacly depends on your environment.
Update: Thus, it's a Java EE webapplication in Eclipse? Assuming that the one is a Dynamic Web Project and the other is a normal Java Project, in the project properties of the Dynamic Web Project you need to add the normal Java Project in the Build Path and the Java EE Module Dependencies.
If that doesn't fix the problem, then most likely the appserver's or the JRE's default classpath is dirty. You'll need to remove any unnecessary project-related libraries from
JRE/lib
,JRE/lib/ext
andTomcat/lib
and promise yourself that you don't touch those library paths anymore ;)您很可能正在针对类路径中的 XClass 的某些 .class 进行编译。但运行时(可能是链接的项目)具有不同版本的 XClass。这是环境特定的。您需要查看编译期间类路径中存在哪些工件以及运行期间拾取的内容;
正如其他人评论的那样。需要有关您的 IDE/构建系统甚至代码的详细信息才能进一步回答。
Very likely that you are compiling against some .class of XClass in your classpath. But the run time (probably linked project) has a different version of XClass. This is environment specific. You need to see what artifacts are in your classpath during compilation and what is being picked up during run;
As commented by others. Details regarding your IDE/Build system or even code is required to further answer.