Rajarshi Das
Rajarshi Das

Reputation: 12320

Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/ObjectMapper on maven

I have one Simple maven app

this is my pom file

<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>example</groupId>
    <artifactId>myapp</artifactId>
    <version>0.0.1-SNAPSHOT</version>



    <packaging>jar</packaging>

    <name>ojminitiator</name>
    <url>http://maven.apache.org</url>



    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.10.1</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.10.1</version>
        </dependency>   
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
            <version>2.10.1</version>
        </dependency>   
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.10.1</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                <defaultGoal>install</defaultGoal>
                <sourceDirectory>src</sourceDirectory> 
                    <archive>
                        <manifest>
                            <mainClass>org.example.App</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

this is what I am trying

import com.fasterxml.jackson.databind.ObjectMapper;

try {
URL url = new URL(uri);
ObjectMapper mapper = new ObjectMapper();
Map<String, ArrayList<Employee>> map = mapper.readValue(url, Map.class);
ArrayList emps = map.get("data");

System.out.println(emps);




}
catch (MalformedURLException e) {

    e.printStackTrace();

  } catch (IOException e) {

    e.printStackTrace();

  }

But I did mvn clean install (build success) and then java -jar target/...snapshot-0.0.1.jar

give the error

Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/ObjectMapper
        at telstra.ojminitiator.App.RestCall(App.java:35)
        at telstra.ojminitiator.App.main(App.java:28)
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.databind.ObjectMapper
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 2 more

Upvotes: 0

Views: 2360

Answers (1)

dan1st
dan1st

Reputation: 16348

You are using maven-jar-plugin to create a jar-with-dependencies.

I think you want to use maven-assembly-plugin instead.

In order to do this, change

<artifactId>maven-jar-plugin</artifactId>

to

<artifactId>maven-assembly-plugin</artifactId>

You may also need to add the execution to the plugin (as pointed out in the comments):

<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>

By the way, you may want to use use package instead of install.

Also execute the jar-with-dependencies and not the "normal" jar:

java -jar target/myApp-jar-with-dependencies.jar

Upvotes: 1

Related Questions