How to Fix a Nosuchmethoderror

How do I fix a NoSuchMethodError?

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.

How to resolve java.lang.NoSuchMethodError

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.

How to fix java.lang.NoSuchMethodError

The problem is not in your test! The test calls ISOPPCServiceHelper which still uses the method getExceptionHandler(). You have to recompile that class. Always use clean test of your build system after refactorings to make sure everything is compiled right (hopefully you have one, otherwise rebuild project of your IDE should do the job).

How to get java.lang.NoSuchMethodError

NoSuchMethodError happens if one class expects a method in another class (and was compiled with that method in place), but at runtime the other class does not have that method. So you need to:

  • create two classes, one of which calls a method on the other
  • compile the two classes
  • then remove the invoked method from the 2nd class, and compile the 2nd class only

Then, if you run the first class (with main method), it will throw that error when trying to call the method on the 2nd class (the method no longer exists)

This example would rarely happen in the real world though. Here are some real cases when the error occurs:

  • You are using a 3rd party library (jar) that depends on another jar. However you are having incompatible versions of those jars, and the 1st one tries to invoke a method on a class in the 2nd
    jar that does not exist anymore/yet.
  • Your compile-time and runtime classpaths differ - you have compiled your code against a version of some library (that can also be the JDK itself), but your runtime is having other versions
  • You have a multi-module project. Your IDE "links" the project at compile-time, so any changes is seen immediately. But when you build you forget to compile one of the modules (where you have added a method), so at runtime it is using the old version.

How can I fix the NoSuchMethodError from the Time4J Library?

A NoSuchMethodError normally means you have a dependency difference between your compile time and runtime environments.

In effect, it's likely that you have either:
1. multiple different versions of the net.time4j JAR on your classpath
2. a different net.time4j JAR on your runtime classpath than was used during compile time.

Since the call hierarchy seems to suggest net.time4j itself is calling a method it doesn't know about, I suspect multiple JAR versions on the classpath.

This seems to be consistent with the JARs you're talking about using. You're mixing version 4.38 and version 5.5. You should stick to using one version of all the libraries in the suite, as they will likely be designed to work together.

You're probably a bit confused because it seems that Time4J changed the packaging between the 4.x and 5.x versions, and time4j-core is probably now named time4j-base

There is a tutorial page on the Time4J website which seems to suggest you can simply remove your existing dependencies on time4-core and time4j-calendar.

How to fix java.lang.NoSuchMethodError even if such a method exist, and error do not occur in local server

The method signature you posted is

void saveOrUpdateReservation(CrentalReservationTO reservation)

but from the exception message

saveOrUpdateReservation(Lcom/smartwcm/commons/reservation/to/CrentalReservationTO;)
Lcom/smartwcm/commons/reservation/to/CrentalReservationTO;

Your code is calling

CrentalReservationTO saveOrUpdateReservation(CrentalReservationTO reservation)

You might have changed a class and not recompiled all dependent classes.

How do I fix a NoSuchMethodError?

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.

java.lang.NoSuchMethodError when the method definitely exists

The problem has now been resolved. Thank you everyone for your assistance.

It turns out that the main project had a dependency which had another MediaDao class, in exactly the same package path. Someone had basically copied the class into that dependency (as a library resource so that lots of projects could use it without specifying the main project as a dependency). However, that someone had not removed the class in the main project.

So, when I modified the class in the main project (I added the updateAlfrescoNodeRef method), and ran the application in STS on my machine, Tomcat used the version of the class in the main project, and not in the library because the library project was closed. When the application was deployed to the server however, it looks like the version of the class in the library was used instead (which, of course, didn't have the updateAlfrescoNodeRef method in it).

Expert tip if you ever find yourself in a similar situation: In STS, press CTRL+SHIFT+T to open the "Open Type" dialog, and enter the name of the problematic class to see a list of projects that have a class with that name.



Related Topics



Leave a reply



Submit