Jackson databind runs in IDE but not in bank

I am trying to use jackson binding using Maven. It works fine in the IDE (I'm using IntelliJ IDEA), but when I try to create a jar with it, it throws an error on startup and crashes instantly. In particular, the error is this:

java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonFactory

      

This is JAR specific and I don't know why this is happening at all. What have I done wrong?

Here is the pom file:

<?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>me.zam</groupId>
    <artifactId>jackson</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.2.3</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-yaml</artifactId>
            <version>2.3.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- Make this jar executable -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <!-- Jar file entry point -->
                            <mainClass>me.Main</mainClass>
                            <addClasspath>true</addClasspath>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

      

And here is the main object

package me;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;

/**
 * Created by Martin on 31/03/2017.
 */
public class Main
{
    public static void main(String[] args)
    {
        ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
        try {
            User user = mapper.readValue(new File("user.yaml"), User.class);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

      

+3


source to share


3 answers


So I just tried to do this on my system and was able to reproduce the problem.

To solve it, you need to add one more plugin maven-shade-plugin

, which is needed to create a thick flask, that is, a flask with all the necessary dependencies.

Update the pom.xml file, notice, here I renamed the class that contains the method main

to Test

and put in the default package: -



 <?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.so</groupId>
        <artifactId>jackson</artifactId>
        <version>1.0-SNAPSHOT</version>

        <packaging>jar</packaging>

        <properties>
            <mainClass>Test</mainClass>
        </properties>

        <dependencies>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>2.2.3</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.dataformat</groupId>
                <artifactId>jackson-dataformat-yaml</artifactId>
                <version>2.3.0</version>
            </dependency>
        </dependencies>

        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-shade-plugin</artifactId>
                    <version>2.4.1</version>
                    <configuration>
                        <createDependencyReducedPom>true</createDependencyReducedPom>
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>${mainClass}</mainClass>
                            </transformer>
                        </transformers>
                        <!-- exclude signed Manifests -->
                        <filters>
                            <filter>
                                <artifact>*:*</artifact>
                                <excludes>
                                    <exclude>META-INF/*.SF</exclude>
                                    <exclude>META-INF/*.DSA</exclude>
                                    <exclude>META-INF/*.RSA</exclude>
                                </excludes>
                            </filter>
                        </filters>
                    </configuration>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>shade</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <!-- Make this jar executable -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <configuration>
                        <archive>
                            <manifest>
                                <!-- Jar file entry point -->
                                <mainClass>Test</mainClass>
                                <addClasspath>true</addClasspath>
                            </manifest>
                        </archive>
                    </configuration>
                </plugin>
            </plugins>
        </build>

    </project>

      

After that, I ran the code by running the first command mvn clean package

, which creates a fat jar, and then ran the code with the command java -cp target/jackson-1.0-SNAPSHOT.jar Test

.

Let me know if you have any problems running the code.

0


source


Can you check if the generated jar contains the specified dependencies like jackson-databind

and jackson-dataformat-yaml

? I'm not sure if the jar plugin exports maven dependencies. I usually use maven build plugin or plugin to create dependency flags.



+1


source


I'm not sure, but you need to change <mainClass>

to <mainClass>

and set the merge scope to like <scope>compile</scope>

in order to add it directly to the jar file.

0


source







All Articles