java com/fasterxml/jackson/databind/ObjectMapper 与 Maven 的 NoClassDefFoundError
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43826718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NoClassDefFoundError of com/fasterxml/Hymanson/databind/ObjectMapper with Maven
提问by hackjutsu
This is a similar question as the one here, which is unfortunately unresolved yet.
这是一个与此处类似的问题,不幸的是尚未解决。
If you want to debug the code, here is the GitHub repo.
如果你想调试代码,这里是GitHub repo。
I got the following NoClassDefFoundError
for ObjectMapper
though I have added the related dependency to Mave pom.xml
.
尽管我已将相关依赖项添加到 Mave NoClassDefFoundError
,ObjectMapper
但我得到了以下信息pom.xml
。
Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/Hymanson/databind/ObjectMapper
at demo.DemoMain.main(DemoMain.java:10)
Caused by: java.lang.ClassNotFoundException: com.fasterxml.Hymanson.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
Here is the source code DemoMain.java
这是源代码 DemoMain.java
package demo;
import com.fasterxml.Hymanson.databind.ObjectMapper;
public class DemoMain {
public static void main(String[] args) {
System.out.println("Start");
ObjectMapper mapper = new ObjectMapper();
System.out.println("End");
}
}
This is my pom.xml
这是我的 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.Hymanson.core</groupId>
<artifactId>Hymanson-annotations</artifactId>
<version>2.8.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.Hymanson.core</groupId>
<artifactId>Hymanson-core</artifactId>
<version>2.8.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.Hymanson.core</groupId>
<artifactId>Hymanson-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>
I compile and run the app by
我编译并运行应用程序
mvn clean install
java -jar target/Demo-1.0-SNAPSHOT.jar
Thanks in advance:)
提前致谢:)
采纳答案by hackjutsu
As I answered here
正如我在这里回答的那样
The default maven plugin doesn't build a fat jar with dependencies.
默认的 maven 插件不会构建具有依赖项的胖 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
.
要构建一个与其依赖项捆绑在一起的 jar 以便我们可以使用 执行它java -jar
,我们可以使用maven-assembly-plugin,它将 jar 打包为名称xxx-jar-with-dependencies.jar
。
Here is a sample pom.xml
这是一个示例 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
现在你应该能够运行你的jar
java -jar xxx-jar-with-dependencies.jar
回答by Dualcoder
<dependency>
<groupId>com.fasterxml.Hymanson.dataformat</groupId>
<artifactId>Hymanson-dataformat-xml</artifactId>
<version>2.8.5</version>
</dependency>