Java NoSuchMethodError when Method Exists
Asked Answered
F

2

8

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!

Frangible answered 17/3, 2017 at 18:8 Comment(20)
Please post the class definition, around this method, with the package statementUnsociable
Are you sure that the JVM that runs your code is in fact using that jar? Not an older version for example? You see, these exceptions don't lie...Burberry
@Unsociable updated.Frangible
@Burberry the system library I am using for my project is JavaSE-1.8, is that not valid?Frangible
No, I mean: YOUR jar could exist in different versions. If your classpath points to an older one, then that could explain that it is inconsistent. Otherwise you have an issue with package names ; or maybe you got more than one version of that util class.Burberry
@Burberry what do you mean my jar could exist in different versions? How could I fix that with Eclipse? It was working fine a couple days ago... Also, there is only one definition of PlayerUtil. I am importing: import net.Swedz.util.PlayerUtil;Frangible
are you able to reference your class Speed from PlayerUtil class?Evyn
Is Player actually in package org.bukkit.entity?Sd
Jar files are files in the file system. You can create as many of them as you want. I am just giving hints; you should do some research on the subjects suggested to you. This is not a free debugging service...Burberry
To add to @AJNeufeld, what is the import line for Player in PlayerUtil.java? Speed expects to send org.bukkit.entity.Player is that what PlayerUtil is expecting to receive?Tamberg
@ShadabFaiz I tried adding a constructor to PlayerUtil, with a String argument, and then ran it in my main method, and I got a NoSuchMethodError...Frangible
@AJneufeld yes, it is.Frangible
@JohnC the import is import org.bukkit.entity.Player; in PlayerUtilFrangible
i meant are you able to create object of Speed class from PlayUtil class? i think it should given cannot find symbol error.Evyn
Edit: Deleted part of my questions that couldn't possibly be happening. As I think about it, I think GhostCat was probably on the right path. How are you running the program? Are you sure the jar file including org.bukkit.entity is on the class path? I'm sure it is in your build path since you can compile, but that doesn't automatically mean it is loaded at runtime.Tamberg
Can you access any of the classes in the org.bukkit.entity at runtime?Tamberg
@Frangible You should try to delete all occurrences of the JAR file, and then compile the whole thing again.Inkberry
@JohnC it is, it's run on a server, it's a plugin for Spigot/BukkitFrangible
@JohnC yes I canFrangible
@MCEmperor I've tried that, I will try again. Tried it, didn't fix it.Frangible
E
17

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.

Eritrea answered 17/3, 2017 at 19:20 Comment(1)
This answer makes totally sense. But for some reason not enough for me, now that I have hit the same issue. I am debugging and analyzing in runtime where my class comes from and, believe it or not, I can perfectly see how the method is contained in the version that gets loaded in runtime. I am going crazy... :O don't know where else to check :(Cappuccino
B
0

I had the same crazy problem, with two servers running the same release package, one worked fine and the other was throwing java.lag.NoSuchMethodError

SOLUTION explicitly list the jars instead of using a wild card path. e.g. when I changed -cp lib/* to -cp lib/base.jar:lib/another.jar:lib/yetanother.jar the noSuchMethod error departed.

Very strange as when running with java -verbose:class it showed a full path to the correct jar file just before NoSuchMethodError. No idea why, just glad to have found a work around.

Bargeboard answered 16/4 at 3:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.