线程“main”java.lang.noclassdeffounderror中出现异常:maven上的com/fasterxml/jackson/databind/objectmapper

cgh8pdjw  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(369)

我有一个简单的maven应用程序
这是我的pom文件

<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>example</groupId>
    <artifactId>myapp</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <packaging>jar</packaging>

    <name>ojminitiator</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.10.1</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.10.1</version>
        </dependency>   
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
            <version>2.10.1</version>
        </dependency>   
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.10.1</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                <defaultGoal>install</defaultGoal>
                <sourceDirectory>src</sourceDirectory> 
                    <archive>
                        <manifest>
                            <mainClass>org.example.App</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

这就是我正在尝试的
导入com.fasterxml.jackson.databind.objectmapper;

try {
URL url = new URL(uri);
ObjectMapper mapper = new ObjectMapper();
Map<String, ArrayList<Employee>> map = mapper.readValue(url, Map.class);
ArrayList emps = map.get("data");

System.out.println(emps);

}
catch (MalformedURLException e) {

    e.printStackTrace();

  } catch (IOException e) {

    e.printStackTrace();

  }

但我做到了 mvn clean install (构建成功),然后是java-jar target/…snapshot-0.0.1.jar
给出错误

Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/ObjectMapper
        at telstra.ojminitiator.App.RestCall(App.java:35)
        at telstra.ojminitiator.App.main(App.java:28)
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.databind.ObjectMapper
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 2 more
hmmo2u0o

hmmo2u0o1#

您正在使用 maven-jar-plugin 创建 jar-with-dependencies .
我想你想用 maven-assembly-plugin 相反。
为了做到这一点,改变

<artifactId>maven-jar-plugin</artifactId>

<artifactId>maven-assembly-plugin</artifactId>

您可能还需要将执行添加到插件中(如注解中所指出的):

<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>

顺便说一下,你可能想用 package 而不是 install .
同时执行 jar-with-dependencies 而不是“正常”的jar:

java -jar target/myApp-jar-with-dependencies.jar

相关问题