tags:

views:

661

answers:

7

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

+1  A: 

This is usually caused when using a build system like Apache Ant that only compiles java files when the java file is newer than the class file. If a method signature changes and classes were using the old version things may not be compiled correctly. The usual fix is to do a full rebuild (usually "ant clean" then "ant").

Sometimes this can also be caused when compiling against one version of a library but running against a different version.

John Meagher
+10  A: 

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.

vetler
+1  A: 

This can also be the result of using reflection. If you have code that reflects on a class and extracts a method by name (eg: with Class.getDeclaredMethod("someMethodName", .....)) then any time that method name changes, such as during a refactor, you will need to remember to update the parameters to the reflection method to match the new method signature, or the getDeclaredMethod call will throw a NoSuchMethodException.

If this is the reason, then the stack trace should show the point that the reflection method is invoked, and you'll just need to update the parameters to match the actual method signature.

In my experience, this comes up occasionally when unit testing private methods/fields, and using a TestUtilities class to extract fields for test verification. (Generally with legacy code that wasn't designed with unit testing in mind.)

rcreswick
+3  A: 

Note that in the case of reflection, you get an NoSuchMethodException, while with non-reflective code, you get NoSuchMethodError. I tend to go looking in very different places when confronted with one versus the other.

erickson
+1  A: 

If you are writing a webapp, ensure that you don't have conflicting versions of a jar in your container's global library directory and also in your app. You may not necessarily know which jar is being used by the classloader.

e.g.

  • tomcat/common/lib
  • mywebapp/WEB-INF/lib
Cheekysoft
A: 

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]

...

matt b
A: 

I feel your pain. You can learn programming out of a book, but when it comes to working with Eclipse or Visual Studio, its a ^&^&'n nightmare to do something simple like add a library. Everybody expects you to know how to use it and if you don't they downvote your question. The problem is, if you don't work in an office or know anyone who you can ask these questions, then its almost impossible to figure this stuff out. Anyway...

I was having your problem, and this is how I fixed it. The following steps are a working way to add a library. I had done the first two steps right, but I hadn't done the last one by dragging the ".jar" file direct from the file system into the "lib" folder on my eclipse project. Additionally, I had to remove the previous version of the library from both the build path and the "lib" folder.

If anyone knows of a more proper way to add/update a library, please chime in.

Step 1 - Add .jar to build path

alt text

Step 2 - Associate sources and javadocs (optional)

alt text

Step 3 - Actually drag .jar file into "lib" folder (not optional)

alt text

DutrowLLC