我正在尝试使用Maven的jackson databind。它在我的IDE环境中运行正常(我正在使用IntelliJ IDEA),但是当我尝试用它制作一个jar时,它会在启动时产生错误并立即崩溃。具体来说,错误是这样的:
java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonFactory
这是JAR特有的,我不知道为什么会这样做。 我做错了什么?
这是pom文件:
<?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>
这是主要对象
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();
}
}
}
答案 0 :(得分:5)
所以我只是在我的系统中尝试过并且能够模拟这个问题。
要解决此问题,您需要再添加一个插件maven-shade-plugin
,这是创建胖罐所必需的,即。具有所有必需依赖性的jar。
更新pom.xml文件,请注意我已将包含main方法的类重命名为Test
并放入默认包中: -
<?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>
完成后,我通过运行首先运行mvn clean package
命令来运行代码,该命令创建胖jar,然后通过java -cp target/jackson-1.0-SNAPSHOT.jar Test
命令运行代码。
如果您在运行代码时遇到任何问题,请与我们联系。
答案 1 :(得分:1)
您能否检查生成的jar是否包含指定的依赖项,例如jackson-databind
和jackson-dataformat-yaml
?我不确定jar插件是否导出了maven依赖项。我通常使用maven程序集插件或者插件来生成带有依赖关系的jar。
答案 2 :(得分:0)
我不确定,但您需要将<mainClass>
更改为<MainClass>
,并将依赖项的范围设置为<scope>compile</scope>
,以将其直接添加到jar文件中。