Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot run code due to java.lang.NoClassDefFoundError: com/fasterxml/jackson/annotation/JsonMerge

I have some test code in Intellij Idea 2018 community edition, which has multiple pom files. When I run any testng annotated test, I get an error which says "no tests were found". It looks like the problem is due to this part of the exception stack trace:

java.lang.NoClassDefFoundError: com/fasterxml/jackson/annotation/JsonMerge

I googled for a solution and found this - https://github.com/FasterXML/jackson-annotations/issues/119 and this https://stackoverflow.com/a/46406070. It looks like this issue is caused when we don't have the same version of these jars in the project.

1) jackson-core (2.8.8)

2) jackson-databind (2.9.2)

3) jackson-annotations (2.8.5)

As you can see, I don't have the same version for all the jars. I looked at all the poms in my project and did not find any place where all these dependencies are added. I was hoping to simply set the version number there. Should I simply add all dependencies in my parent pom file or do something else ?

How do I resolve this issue without harming the project ? How do I find out why these jars are not of the same version ?

like image 826
MasterJoe Avatar asked Aug 26 '18 07:08

MasterJoe


2 Answers

You most likely have different versions imported through different dependencies as sub-dependencies.

You can get maven to show you the so-called "effective pom" which will give you the full dependency tree, from which you can then see where what's included.

Some IDEs (like IntelliJ) have an option to show this graphically, which makes finding conflicts like this a lot easier.

Exclude lower versions, and if required explicitly add dependencies to newer versions.

like image 96
jwenting Avatar answered Oct 23 '22 17:10

jwenting


The keyword you are looking for is "Dependency Exclusion". Maven includes transitive dependencies automatically. You first need to identify where the dependencies are coming from.

You can redirect the output to a file and analyze it in detail by searching for "jackson" in the tree.txt file generated as follows:

mvn dependency:tree -Dverbose > tree.txt

Next step would be find out whether you can upgrade some of your libraries so that they automatically use the right version of jackson libraries for you.

Finally, if you explicitly want to exclude transitive dependencies, you can use <exclusions> tag inside a certain <dependency> to exclude certain third party dependencies added to your classpath. See this SO question, for example.

like image 25
Jaywalker Avatar answered Oct 23 '22 18:10

Jaywalker