Jackson 数据绑定在 IDE 环境中工作但不在 jar 中
Jackson databind working in IDE environment but not in jar
我正在尝试通过 Maven 使用 jackson 数据绑定。它在我的 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();
}
}
}
我不确定,但您需要将 <mainClass>
更改为 <MainClass>
并设置依赖项的范围以像 <scope>compile</scope>
一样编译以将其直接添加到 jar 文件中。
能否请您检查生成的jar是否包含指定的依赖项,例如jackson-databind
和jackson-dataformat-yaml
?我不确定 jar 插件是否导出了 Maven 依赖项。我一般使用maven assembly plugin或者shade plugin生成带依赖的jar
所以我刚刚在我的系统中尝试了它并且能够重现该问题。
要解决它,您需要再添加一个插件 maven-shade-plugin
,这是创建 fat jar 所必需的,即具有所有必需依赖项的 Jar。
更新pom.xml文件,注意这里我已经将包含main
方法的class重命名为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
命令的代码,它创建 fat jar 然后 运行 通过 java -cp target/jackson-1.0-SNAPSHOT.jar Test
命令。
如果您在代码中遇到任何问题,请告诉我运行。
我正在尝试通过 Maven 使用 jackson 数据绑定。它在我的 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();
}
}
}
我不确定,但您需要将 <mainClass>
更改为 <MainClass>
并设置依赖项的范围以像 <scope>compile</scope>
一样编译以将其直接添加到 jar 文件中。
能否请您检查生成的jar是否包含指定的依赖项,例如jackson-databind
和jackson-dataformat-yaml
?我不确定 jar 插件是否导出了 Maven 依赖项。我一般使用maven assembly plugin或者shade plugin生成带依赖的jar
所以我刚刚在我的系统中尝试了它并且能够重现该问题。
要解决它,您需要再添加一个插件 maven-shade-plugin
,这是创建 fat jar 所必需的,即具有所有必需依赖项的 Jar。
更新pom.xml文件,注意这里我已经将包含main
方法的class重命名为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
命令的代码,它创建 fat jar 然后 运行 通过 java -cp target/jackson-1.0-SNAPSHOT.jar Test
命令。
如果您在代码中遇到任何问题,请告诉我运行。