How do I fix a NoSuchMethodError?

2018-12-31 01:41发布

I'm getting a NoSuchMethodError error when running my Java program. What's wrong and how do I fix it?

25条回答
旧人旧事旧时光
2楼-- · 2018-12-31 02:16

I've encountered this error too.

My problem was that I've changed a method's signature, something like

void invest(Currency money){...}

into

void invest(Euro money){...}

This method was invoked from a context similar to

public static void main(String args[]) {
    Bank myBank = new Bank();

    Euro capital = new Euro();
    myBank.invest(capital);
}

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:

 #7 = Methodref          #2.#22         // Bank.invest:(LCurrency;)V

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.

查看更多
低头抚发
3楼-- · 2018-12-31 02:17

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.

查看更多
浅入江南
4楼-- · 2018-12-31 02:23

If you have access to change the JVM parameters, adding verbose output should allow you to see what classes are being loaded from what JARs.

java -verbose:class <other args>

When your program is run, the JVM should dump to standard out information such as:

...

[Loaded junit.framework.Assert from file:/C:/Program%20Files/junit3.8.2/junit.jar]

...

查看更多
怪性笑人.
5楼-- · 2018-12-31 02:23

I ran into a similar problem when I was changing method signatures in my application. Cleaning and rebuilding my project resolved the "NoSuchMethodError".

查看更多
看淡一切
6楼-- · 2018-12-31 02:23

Most of the times java.lang.NoSuchMethodError is caught be compiler but sometimes it can occur at runtime. If this error occurs at runtime then the only reason could be the change in the class structure that made it incompatible.

Best Explanation: https://www.journaldev.com/14538/java-lang-nosuchmethoderror

查看更多
不流泪的眼
7楼-- · 2018-12-31 02:24

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.

查看更多
登录 后发表回答