2

我正在尝试使用 Maven 使用杰克逊数据绑定。它在我的 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();
        }
    }
}
4

3 回答 3

1

能否请您检查生成的jar 是否包含指定的依赖项,例如jackson-databindand jackson-dataformat-yaml?我不确定 jar 插件是否导出了 maven 依赖项。我通常使用 maven 程序集插件或 shade 插件来生成具有依赖关系的 jar。

于 2017-04-01T07:28:16.690 回答
1

所以我只是在我的系统中尝试了它并且能够重现该问题。

要解决这个问题,您需要再添加一个插件maven-shade-plugin,这是创建胖 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 Test命令运行代码。

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

于 2017-04-01T08:48:30.003 回答
0

我不确定,但您需要更改<mainClass><MainClass>设置依赖项的编译范围,例如<scope>compile</scope>将其直接添加到 jar 文件中。

于 2017-04-01T07:17:43.573 回答