Java NoSuchMethodError when Method Exists

人走茶凉 提交于 2019-12-04 23:59:07

问题


I am referencing PlayerUtil.getMovementSpeed(player); in my Speed class, and in my PlayerUtil class, I have the method defined as:

public static double getMovementSpeed(Player player) {
  //my code here
}

But whenever the getMovementSpeed method is referenced in my other classes, it throws this error:

java.lang.NoSuchMethodError: net.Swedz.util.PlayerUtil.getMovementSpeed(Lorg/bukkit/entity/Player;)D

I thought it may be that Eclipse was exporting incorrectly, but I rebooted it and tried again with no avail.

EDIT: I did try decompiling the exported jar, and the public static double getMovementSpeed(Player player) method does exist in the exported jar.

EDIT: My friend is also having a similar issue, and is using IntelliJ, so Eclipse is not the issue.

EDIT: Class definition for PlayerUtil:

package net.Swedz.util;

public class PlayerUtil implements Listener {
    //getMovementSpeed is defined in here
}

Class definition for Speed:

package net.Swedz.hack.detect.move;

public class Speed implements Hack, Listener {
    //my detection methods and method containing PlayerUtil.getMovementSpeed(player);
}

SOLUTION: I found on my own that I had classes conflicting between two plugins on my server. I had one jar with net.Swedz.util.PlayerUtil and another with net.Swedz.util.PlayerUtil both with different contents. I added my project name in all lower case after the net.Swedz and it seems to have fixed it!

Thanks!


回答1:


This is a very simple to troubleshoot. you have used that method and you were able to compile that class which uses this method.

so that means at compile time it reefers the class PlayerUtil which has this method.

But runtime class loader has loaded the class PlayerUtil which doesn't contain this method. now what you have to do is just find out where that class has been loaded from (at run time)

if you can recreate the problem while it is running using eclipse/IDEA follow these steps. (if it runs in in application server or standalone application, then start the application server or application with debug enabled.and you can do remote debug from your IDE).

  1. put a break-point where exception was thrown (where you call this method).
  2. start to debug , it will hit the break-point.
  3. then evaluate this expression PlayerUtil.class.getResource("PlayerUtil.class")

4.you can find the path where the class was loaded from.

now you have two options , decompile the class and check whether that method is these (same return type, same name , same args).

or in debug , you can evaluate PlayerUtil.class.getDeclaredMethods() to find out.

So you can solve the problem by rectifying the class path entries if it was loaded from a wrong place.



来源:https://stackoverflow.com/questions/42864427/java-nosuchmethoderror-when-method-exists

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!