java.lang.ClassNotFoundException / NoClassDefFoundError for com/fasterxml/jackson/databind/ObjectMapper with Maven
Asked Answered
B

2

18

I get the following error when trying to run a java program that uses jackon's ObjectMapper class:

Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/ObjectMapper
  at com.inin.dynamotransfer.DynamoTransfer.importData(DynamoTransfer.java:133)
  at com.inin.dynamotransfer.DynamoTransfer.main(DynamoTransfer.java:67)
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.databind.ObjectMapper
  at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
  at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
  at java.security.AccessController.doPrivileged(Native Method)
  at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
  ... 2 more

I have the Jackson annotations, databind (which contains ObjectMapper), and core JARs in my Maven repository under ~/.m2/repository/com/fasterxml/jackson/core/jackson-[packagename]/2.4.0. here's the dependencies section of my pom.xml:

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

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.4.0</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.4.0</version>
    </dependency>
</dependencies>

here's my CLASSPATH:

:/Users/myusername/Library/Java/Extensions:/Library/Java/Extensions:/Users/myusername/.m2/repository

so Maven knows all about these JARs - where to get em, where to put em, etc. - but java itself can't seem to find the classes I need. what am I doing wrong?? thanks!

Boneblack answered 26/9, 2014 at 20:52 Comment(2)
have a look at #18429968Barbwire
thanks Frederic. I solved my problem by copying the Jackson JARs to ~/Library/Java/Extensions. I'm still not sure why I was able to run my program previously, without having done that. I guess Maven uses its JAR repository, when you execute a program via the exec plugin, but the java command doesn't. that also doesn't explain why java couldn't find Jackson when I passed the location of the JARs via the command line -cp option.Boneblack
A
5

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
Astronomical answered 7/5, 2017 at 2:3 Comment(0)
G
4

try to redo maven lifecycle goals:

mvn clean install -U
Galling answered 18/8, 2015 at 21:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.