NoClassDefFoundError of com/fasterxml/jackson/databind/ObjectMapper with Maven
Asked Answered
T

2

16

This is a similar question as the one here, which is unfortunately unresolved yet.

If you want to debug the code, here is the GitHub repo.

I got the following NoClassDefFoundError for ObjectMapper though I have added the related dependency to Mave pom.xml.

Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/ObjectMapper
    at demo.DemoMain.main(DemoMain.java:10)
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.databind.ObjectMapper
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 1 more

Here is the source code DemoMain.java

package demo;

import com.fasterxml.jackson.databind.ObjectMapper;

public class DemoMain {
    public static void main(String[] args) {
        System.out.println("Start");
        ObjectMapper mapper = new ObjectMapper();
        System.out.println("End");
    }
}

This is my pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>Demo</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.8.3</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.8.3</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.8.3</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.0.0</version>
            <configuration>
                <archive>
                    <index>true</index>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>demo.DemoMain</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

I compile and run the app by

mvn clean install
java -jar target/Demo-1.0-SNAPSHOT.jar
Traject answered 7/5, 2017 at 0:39 Comment(8)
Does the mvn clean install build success?Esoteric
Yes, mvn clean install is successful.Traject
It's too weird! Maven build successful mean that your code is not having any problem. Did you try to build it using an IDE?Esoteric
@TuyenNguyen I'm confused too. Here is the source code repository. github.com/hackjutsu/StackOverflow-Maven-JacksonTraject
you should try to using an IDE to build and run your code. I have tested your code, it run fine!Esoteric
Thanks for trying:) Yes, I just tried using IntelliJ and it worked fine. But an IDE shouldn't be a prerequisite for a Java application... Is it because I miss some flags when launching the jar?Traject
I just figure out that mvn clean install does not create any .jar file. So what .jar file you are running on?Esoteric
En.. Interesting, it should create a jar file target/Demo-1.0-SNAPSHOT.jar. Anyway, I figure out the issue. The maven plugin I was using doesn't build a fat jar that comes with dependencies. To run the jar with java -jar, we can use maven-assembly-plugin instead, which bundles the jar with its dependencies. By the way, the reason why an IDE works is because it adds the classpath flag automatically when running the app. Thanks for helping me troubleshoot.Traject
T
10

As I answered here


The default maven plugin doesn't build a fat jar with dependencies.

To build a jar bundled with its dependencies so that we can execute it with java -jar, we can use maven-assembly-plugin, which packages the jar with the name xxx-jar-with-dependencies.jar.

Here is a sample pom.xml

<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.0.0</version>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>com.example.yourMain</mainClass>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id> <!-- this is used for inheritance merges -->
                    <phase>package</phase> <!-- bind to the packaging phase -->
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Now you should be able to run your jar with

java -jar xxx-jar-with-dependencies.jar
Traject answered 7/5, 2017 at 2:55 Comment(0)
B
12
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.8.5</version>
</dependency>
Buddybuderus answered 7/5, 2017 at 6:16 Comment(2)
try this dependancyBuddybuderus
You should add an actual explanation in your answer, otherwise, it's just you pasting some code which the poster might not understand.Spatula
T
10

As I answered here


The default maven plugin doesn't build a fat jar with dependencies.

To build a jar bundled with its dependencies so that we can execute it with java -jar, we can use maven-assembly-plugin, which packages the jar with the name xxx-jar-with-dependencies.jar.

Here is a sample pom.xml

<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.0.0</version>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>com.example.yourMain</mainClass>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id> <!-- this is used for inheritance merges -->
                    <phase>package</phase> <!-- bind to the packaging phase -->
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Now you should be able to run your jar with

java -jar xxx-jar-with-dependencies.jar
Traject answered 7/5, 2017 at 2:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.