gpt4 book ai didi

java - Jackson 数据绑定(bind)在 IDE 环境中工作但不在 jar 中

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:50:49 26 4
gpt4 key购买 nike

我正在尝试通过 Maven 使用 jackson 数据绑定(bind)。它在我的 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();
}
}
}

最佳答案

所以我只是在我的系统中试了一下,并且能够重现这个问题。

要解决它,您需要再添加一个插件 maven-shade-plugin,这是创建 fat jar 所必需的,即具有所有必需依赖项的 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 命令来运行代码,该命令创建 fat jar,然后通过 java -cp target/jackson-1.0-SNAPSHOT 运行代码。 jar 测试命令。

如果您在运行代码时遇到任何问题,请告诉我。

关于java - Jackson 数据绑定(bind)在 IDE 环境中工作但不在 jar 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43153939/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com