Java 包、项目、NoSuchMethod 错误

发布于 2024-08-29 05:02:40 字数 300 浏览 0 评论 0原文

我有两个链接的项目,这两个项目下都有一个包。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

滥情哥ㄟ 2024-09-05 05:02:40

来自 java.lang.NoSuchMethodErrorjavadoc

如果应用程序尝试调用类(静态或实例)的指定方法,并且该类不再具有该方法的定义,则抛出此异常。

通常情况下,这个错误会被编译器捕获;仅当类的定义发生不兼容的更改时,此错误才会在运行时发生。

换句话说,运行时类路径不包含具有所需方法的类,而它在编译时类路径中可用。总结:你的类路径搞砸了。清理并对齐。如何做到这一点完全取决于您的环境。

更新:因此,它是 Eclipse 中的 Java EE Web 应用程序?假设一个是动态Web项目,另一个是普通Java项目,则在动态Web项目的项目属性中您需要在构建路径Java EE 模块依赖项中添加正常的Java 项目

如果这不能解决问题,那么很可能应用程序服务器或 JRE 的默认类路径是脏的。您需要从 JRE/libJRE/lib/extTomcat/lib 中删除任何不必要的项目相关库,并向自己保证你不再接触那些库路径了;)

From the java.lang.NoSuchMethodError javadoc:

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.

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 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 and Tomcat/lib and promise yourself that you don't touch those library paths anymore ;)

℡Ms空城旧梦 2024-09-05 05:02:40

您很可能正在针对类路径中的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文