1

I have a project that I can run correctly from within Eclipse, but running the standalone jar gives me the java.lang.NoSuchMethodError for a class at runtime.

I unpacked the jar file and can see the class file in one library file in jar and I do see the method defined in the class. The exception is not about class not found, but just can't find the method in the class.

Could not find any helpful information on the internet for how to solve it.

Ravi
  • 30,829
  • 42
  • 119
  • 173
user2777473
  • 3,736
  • 5
  • 26
  • 39

2 Answers2

7

It is much likely that there is another jar in the dependencies that has the same class(may be another version for the same library) but don't have this method. I think that this latter class is the one loaded instead of the one you unpacked.

Hamdi Douss
  • 1,033
  • 1
  • 8
  • 17
  • I expanded all the jars within that executable jar. I found two class files for the class. decompile them and both of them have the same method defined. – user2777473 Jan 12 '18 at 21:10
  • Are you sure the two methods has the same signature? – Hamdi Douss Jan 12 '18 at 21:11
  • Try to take out one class at a time from the executable jar, just to validate the analysis. – Hamdi Douss Jan 12 '18 at 21:13
  • 1
    Yes, both of them have the same signature. I also tried to remove one of the class and keep only one copy in the jar, but it does not solve the issue. – user2777473 Jan 13 '18 at 02:01
1

NoSuchMethodError occurs when

... 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

As highlighted, there are two possibilities :

  1. Either method definition doesn't exists
  2. If method definition exist, then they might have incompatible change i.e. you might be using different version of jar/class during runtime.
Ravi
  • 30,829
  • 42
  • 119
  • 173