java.lang.ClassNotFoundException / NoClassDefFoundError for com/fasterxml/jackson/databind/ObjectMapper with Maven

2022-09-03 07:56:56

当我尝试运行使用jackon的ObjectMapper类的java程序时,我收到以下错误:

Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/ObjectMapper
  at com.inin.dynamotransfer.DynamoTransfer.importData(DynamoTransfer.java:133)
  at com.inin.dynamotransfer.DynamoTransfer.main(DynamoTransfer.java:67)
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.databind.ObjectMapper
  at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
  at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
  at java.security.AccessController.doPrivileged(Native Method)
  at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
  ... 2 more

我在 Maven 存储库中拥有 Jackson 注释、databind(包含 ObjectMapper)和核心 JAR,位于 ~/.m2/repository/com/fasterxml/jackson/core/jackson-[packagename]/2.4.0 下。这是我的pom的依赖部分.xml:

<dependencies>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.4.0</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.4.0</version>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.4.0</version>
    </dependency>
</dependencies>

这是我的类路径:

:/Users/myusername/Library/Java/Extensions:/Library/Java/Extensions:/Users/myusername/.m2/repository

所以 Maven 知道所有关于这些 JAR 的信息 - 从哪里获取 em,在哪里放置 em,等等 - 但 java 本身似乎找不到我需要的类。我做错了什么??谢谢!


答案 1

尝试重做 Maven 生命周期目标:

mvn clean install -U

答案 2

默认的 maven 插件不会构建具有依赖项的胖 jar。

要构建一个与其依赖项捆绑在一起的jar,以便我们可以使用 执行它,我们可以使用maven-assembly-plugin,它将jar打包为名称。java -jarxxx-jar-with-dependencies.jar

下面是一个示例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>

现在你应该能够运行你的罐子

java -jar xxx-jar-with-dependencies.jar

推荐