使用Maven的com / fasterxml / jackson / databind / ObjectMapper的NoClassDefFoundError [英] NoClassDefFoundError of com/fasterxml/jackson/databind/ObjectMapper with Maven

查看:271
本文介绍了使用Maven的com / fasterxml / jackson / databind / ObjectMapper的NoClassDefFoundError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个与此处类似的问题,但遗憾的是尚未解决。

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


如果你想调试代码,这里是 GitHub repo

我得到以下 NoClassDefFoundError for ObjectMapper 虽然我已将相关依赖项添加到Mave pom.xml

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

这是源代码 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");
    }
}

这是我的 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>

我编译并运行应用程序by

I compile and run the app by

mvn clean install
java -jar target/Demo-1.0-SNAPSHOT.jar

提前致谢:)

推荐答案

我回答此处

默认的maven插件不构建带有依赖项的胖jar。

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

构建一个与其依赖项捆绑在一起的jar,以便我们可以执行它使用 java -jar ,我们可以使用 maven-assembly-plugin ,它使用名称 xxx-jar-with-dependencies.jar 打包jar。

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.

这是一个示例 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>

现在你应该可以运行你的jar了

Now you should be able to run your jar with

java -jar xxx-jar-with-dependencies.jar

这篇关于使用Maven的com / fasterxml / jackson / databind / ObjectMapper的NoClassDefFoundError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆