首页 > 解决方案 > java.lang.NoClassDefFound 与 Maven 错误

问题描述

大家好,提前感谢您的帮助。我是一名学生,正在学习java。这个学期我们有一个小组项目,我们需要完成一个已经实现了一些功能的代码。我的主要任务是在目录分配的类分配中实现一个算法,主要使用目录数据模型中类的函数。尽管在不同目录中的每个类都在同一个包中。从数据模型类中调用静态字段时,我没有遇到任何问题。但是,当在这里调用方法时,我得到错误 ClassDefNotFound。我要补充一点,我使用 .jar 文件运行程序。我尝试了很多方法来解决这个问题,但不幸的是我还没有解决它,而且我的时间已经不多了,所以我在这里寻求你的帮助。

我尝试通过添加配置来更改分配目录中的 pom.xml 文件,但它仍然无法正常工作。此外,我对应该写下哪条路径感到困惑。我知道它是一个绝对路径,但我不能写我的用户目录,因为该项目应该在任何计算机上工作。

再次感谢您未来的帮助。我一直在努力解决这件事,所以解决这个问题的任何帮助都可以挽救生命!

标签: javamavenclasspathnoclassdeffounderror

解决方案


认为您在生成 jar 时缺少依赖项。这就是为什么它说这个类没有定义,在你的 pom.xml 中试试这个

来源: 使用 Maven 在 jar 中包含依赖项

<build>
    <plugins>
      <!-- any other plugins -->
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
      </plugin>
    </plugins>
  </build>

个人 pom.xml 示例(注意在开始使用上述插件时,我还定义了一个清单,尝试两种方式:

<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>NuevaUtilidadExcel</groupId>
    <artifactId>NuevaUtilidadExcel</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.0.0</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>com.nuevaUtilidad.vista.InterfazPrincipal</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>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <id>unknown-jars-temp-repo</id>
            <name>A temporary repository created by NetBeans for libraries and jars it could not identify. Please replace the dependencies in this repository with correct ones and delete this repository.</name>
            <url>file:${project.basedir}/lib</url>
        </repository>
        <repository>
            <id>netbeans</id>
            <name>Netbeans rep</name>
            <url>http://bits.netbeans.org/maven2/</url>
        </repository>

    </repositories>
    <dependencies>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.0</version>
        </dependency>

        <dependency>
            <groupId>com.opencsv</groupId>
            <artifactId>opencsv</artifactId>
            <version>4.0</version>
        </dependency>


        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.9.6</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.9</version>
        </dependency>

        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.6</version>
        </dependency>

        <dependency>
            <groupId>org.apache.xmlbeans</groupId>
            <artifactId>xmlbeans</artifactId>
            <version>3.0.1</version>
        </dependency>

        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0</version>
        </dependency>

        <dependency>
            <groupId>com.toedter</groupId>
            <artifactId>jcalendar</artifactId>
            <version>1.4</version>
        </dependency>

    </dependencies>
    <name>NuevaUtilidadExcelSVG</name>
</project>

推荐阅读