The java.lang.NoSuchMethodError

Hey there, Allan here.

Quick post, for the begginers out there:

I think anyone who programmed for more than one month with a Java + Maven project already saw that error. The basic scenario is simple:

You got your project compiling and building nice and easy. You lay back, relax a bit, and write some tests, go to youtube, refactor some method, stare at your screen and admire your work.

Damn, Im awesome!

Damn, Im awesome!

After a while you decide to do a full test. You pack your code, throw that in your server, restart, test and voila…

java.lang.NoSuchMethodError

You just panic! How in the world is that possible?? You were sure of everything! And then you spend some time to figure it out what’s wrong.

The Answer

Dependency conflict. ALMOST aways that is the case: you added some dependency that is dependent to some other dependency from your project, with a different version. Let me illustrate:

Dependency ConflictIn this case, you depend on log4j-1.2.13, and when you add commons-logging-1.1, it will bring as a transitive dependency log4j-1.2.12. Bam! Error! Panic! Crying!

It’s not overly complicated to solve this, but, can be troublesome because there’s no easy way out here, you got to check your pom for dependency conflicts. Eclipse got a great maven plugin, that helps you out, but in case you want:

mvn dependency:tree

This will show you a complete tree of your dependencies. With that in hand, you can search for dependencies that caused your headache.

When you find the problem, the best way is to exclude the transitive dependency.

Hope it helps!

Leave a message

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s