com/fasterxml/jackson/databind/ObjectMapper 与 Maven 的 NoClassDefFoundError

编程入门 行业动态 更新时间:2024-10-24 21:21:11
本文介绍了com/fasterxml/jackson/databind/ObjectMapper 与 Maven 的 NoClassDefFoundError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是一个与此处类似的问题,不幸的是尚未解决.

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

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

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

我为 ObjectMapper 得到了以下 NoClassDefFoundError,尽管我已经将相关的依赖添加到了 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.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="maven.apache/POM/4.0.0" xmlns:xsi="www.w3/2001/XMLSchema-instance" xsi:schemaLocation="maven.apache/POM/4.0.0 maven.apache/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>

我编译并运行应用程序

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>

现在你应该可以用

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

更多推荐

com/fasterxml/jackson/databind/ObjectMapper 与 Maven 的 NoClassDefFoundError

本文发布于:2023-05-25 10:53:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/226599.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:databind   jackson   fasterxml   NoClassDefFoundError   Maven

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!