简体   繁体   English

为什么即使在POM中包含正确的依赖项后,我也得到NoClassDefFoundError?

[英]Why I am getting NoClassDefFoundError even after I have included right dependencies in POM?

I am trying to build my application and ship the jar file to users. 我正在尝试构建我的应用程序并将jar文件发送给用户。 It is bulding fine, but it is giving problem when I am trying to run the jar. 它很好,但是当我试图运行jar时它会产生问题。

I am trying to run jar file compiled from mvn package command. 我正在尝试运行从mvn package命令编译的jar文件。 After that, I am running java -jar ApplicationRunner.jar , but I am getting below exception. 之后,我正在运行java -jar ApplicationRunner.jar ,但我得到的是异常。 Note that when I am running through Intellij Idea IDE - it works fine 请注意,当我通过Intellij Idea IDE运行时 - 它工作正常

Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonProcessingException
    at cs550.pa3.ApplicationRunner.<init>(ApplicationRunner.java:27)
    at cs550.pa3.ApplicationRunner.main(ApplicationRunner.java:39)
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.core.JsonProcessingException
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

My pom.xml file - 我的pom.xml文件 -

<?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>FileSharingSystem</groupId>
    <artifactId>FileSharingSystem-1</artifactId>
    <version>3.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <!-- Make this jar executable -->
            <plugin>

                <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-jar-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>cs550.pa3.ApplicationRunner</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
            </plugin>
        </plugins>

    </build>
    <dependencies>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.5</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.mockito/mockito-all -->
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>1.9.5</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>RELEASE</version>
        </dependency>
        <!--<dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.6.1</version>
        </dependency>-->

        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
        <!--<dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.8.4</version>
        </dependency>-->

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.8.0</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.8.0</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.8.0</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-jsr310</artifactId>
            <version>2.6.5</version>
        </dependency>


    </dependencies>

</project>

If you want to provide an executable jar to the client you should add the required dependencies in the jar. 如果要向客户端提供可执行jar,则应在jar中添加所需的依赖项。
You could use the maven-assembly-plugin to create a jar including dependencies : 您可以使用maven-assembly-plugin创建一个包含依赖项的jar:

<build>
  <plugins>
    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <configuration>
        <archive>
          <manifest>
            <mainClass>cs550.pa3.ApplicationRunner</mainClass>
          </manifest>
        </archive>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
      </configuration>
    </plugin>
  </plugins>
</build>

As @davidxxx mentioned, we need to build it with dependencies. 正如@davidxxx所提到的,我们需要使用依赖项来构建它。 For future reference it is below - 以下参考如下 -

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>FileSharingSystem</groupId>
    <artifactId>FileSharingSystem</artifactId>
    <version>3.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>File Sharing System</name>
    <url>http://maven.apache.org</url>


    <properties>
        <jdk.version>1.8</jdk.version>
        <jodatime.version>2.5</jodatime.version>
        <junit.version>4.12</junit.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>1.9.5</version>
        </dependency>
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>${jodatime.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.8.0</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.8.0</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.8.0</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-jsr310</artifactId>
            <version>2.6.5</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>RELEASE</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>FileSharingSystem</finalName>
        <plugins>

            <!-- download source code in Eclipse, best practice -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>false</downloadJavadocs>
                </configuration>
            </plugin>

            <!-- Set a compiler level -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>${jdk.version}</source>
                    <target>${jdk.version}</target>
                </configuration>
            </plugin>

            <!-- Maven Assembly Plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4.1</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <!-- MainClass in mainfest make a executable jar -->
                    <archive>
                        <manifest>
                            <mainClass>cs550.pa3.ApplicationRunner</mainClass>
                        </manifest>
                    </archive>

                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase> <!-- bind to the packaging phase -->
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

</project>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 即使我已经在 POM.xml 中包含了所有依赖项,我在“mvn install”时也会收到符号未找到错误 - Even if I have included all the dependencies in POM.xml, I am getting symbol not found error while “mvn install” 部署后为什么会出现noclassdeffounderror? - why am i getting the noclassdeffounderror after deploying? 为什么我得到NoClassDefFoundError? - Why am I getting a NoClassDefFoundError? 为什么会出现NoClassDefFoundError? - Why am I getting NoClassDefFoundError? 为什么会出现此NoClassDefFoundError? - Why am I getting this NoClassDefFoundError? 为什么我必须在父pom中添加依赖项? - Why I have to add dependencies with parent pom? java.lang.NoClassDefFoundError:org / slf4j / LoggerFactory即使我有正确的依赖关系 - java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory even though I have the right dependencies 即使CommonTermsQuery包含在jar中我也能得到NoClassDefFoundError,我可以在构建路径中看到它。 可能是什么问题? - I am getting NoClassDefFoundError even when CommonTermsQuery is included in the jar and i can see it in the build path. What could be the issue? 为什么我在 pdfXFA 示例中收到 NoClassDefFoundError 错误? - Why am I getting a NoClassDefFoundError in the pdfXFA example? 为什么我收到NoClassDefFoundError / ClassNotFoundException - Why am I getting NoClassDefFoundError / ClassNotFoundException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM