开发者

Java Package, Project , NoSuchMethod error

开发者 https://www.devze.com 2022-12-27 14:12 出处:网络
I have 2 projects linked and these 2 projects each have a package under them. XProject -> XPackage -> XClass -> X1Method(); X2Method();

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开发者_C百科 would be appreciated. Thanks.


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 ;)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消