方法存在时的Java NoSuchMethodError [英] Java NoSuchMethodError when Method Exists

查看:154
本文介绍了方法存在时的Java NoSuchMethodError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的Speed类中引用 PlayerUtil.getMovementSpeed(player); ,在我的PlayerUtil类中,我将方法定义为:

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
}

但是无论何时在我的其他类中引用getMovementSpeed方法,它都会抛出此错误:

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

我认为可能是Eclipse导出错误,但我重新启动它并再次尝试无效。

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

编辑:我确实尝试反编译导出的jar,导出的jar中确实存在 public static double getMovementSpeed(Player player)方法。

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

编辑:我的朋友也有类似的问题,并且正在使用IntelliJ,所以Eclipse不是问题。

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

编辑: PlayerUtil的类定义:

Class definition for PlayerUtil:

package net.Swedz.util;

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

Speed的类定义:

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

解决方案:我自己找到了我我的服务器上的两个插件之间有类冲突。我有一个带有 net.Swedz.util.PlayerUtil 的jar和另一个带有 net.Swedz.util.PlayerUtil 的jar不同的内容。我在 net.Swedz 之后添加了我的项目名称全部小写,它似乎修复了它!

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!

谢谢!

推荐答案

这是一个非常简单的故障排除方法。
您已经使用了该方法,并且您能够编译使用此方法的类。

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

这意味着在编译时它会将类PlayerAtil引用到这个方法。

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

但运行时类加载器已经加载了不包含此方法的类PlayerUtil。
现在您要做的就是找出该类的加载位置(在运行时)

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)

如果您可以重新创建问题使用eclipse / IDEA运行遵循这些步骤。
(如果它在应用程序服务器或独立应用程序中运行,然后在启用调试的情况下启动应用程序服务器或应用程序。您可以从IDE进行远程调试。)

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. 设置了抛出异常的断点(你调用这个方法的地方)。

  2. 开始调试,它将达到断点。

  3. 然后评估此表达式 PlayerUtil.class.getResource(PlayerUtil.class)

  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.你可以找到加载类的路径。

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

现在你有两个选项,反编译类,并检查该方法是否是这些(相同的返回类型,相同的名称,相同的args)。

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

或在调试中,您可以评估 PlayerUtil。 class.getDeclaredMethods()找出来。

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.

这篇关于方法存在时的Java NoSuchMethodError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆