You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Maven生成Jar未包含依赖,运行报ClassNotFoundException求助

问题描述

构建过程成功生成了Jar文件,但运行时抛出ClassNotFoundException,提示找不到com.javarush.games.racer.RacerGame类。

以下是我的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>jru.module3</groupId>
    <artifactId>project-maven</artifactId>
    <version>1.0</version>

    <properties>
        <maven.compiler.source>18</maven.compiler.source>
        <maven.compiler.target>18</maven.compiler.target>
    </properties>

    <repositories>
        <repository>
            <id>localrep</id>
            <name>local repository</name>
            <url>file:${project.basedir}/lib</url>
        </repository>
    </repositories>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.12.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.openjfx/javafx-controls -->
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>18.0.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.8.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.javarush</groupId>
            <artifactId>desktop-game-engine</artifactId>
            <version>1.0</version>
            <scope>system</scope>
            <systemPath>D:/JDE/IDE/projects/project-maven-master/lib/desktop-game-engine.jar</systemPath>
        </dependency>

    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>
                    D:\JDE\IDE\projects\project-maven-master\lib\
                </directory>
                <includes>
                    <include>*.jar</include>
                </includes>
            </resource>
            <resource>
            <directory>
                D:\JDE\IDE\projects\project-maven-master\src\main\java\com\javarush\games\racer\
            </directory>
            <includes>
                <include>*.class</include>
            </includes>
        </resource>
        </resources>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.11.0</version>
                <configuration>
                    <source>18</source>
                    <target>18</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.0</version>
                <executions>
                    <execution>
                        <id>default</id>
                        <phase>package</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                        <configuration>

                        </configuration>
                    </execution>
                </executions>
                    <configuration>
                    <archive>
                        <manifestFile>D:\JDE\IDE\projects\project-maven-master\resources\MANIFEST.MF</manifestFile>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib</classpathPrefix>
                            <mainClass>org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader</mainClass>
                        </manifest>
                        <manifestEntries>
                            <Rsrc-Main-Class>com.javarush.games.racer.RacerGame</Rsrc-Main-Class>
                        </manifestEntries>
                    </archive>
                    </configuration>

            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-install-plugin</artifactId>
                <version>3.1.1</version>
                <configuration>
                    <groupId>com.javarush</groupId>
                    <artifactId>desktop-game-engine</artifactId>
                    <version>1.0</version>
                    <packaging>jar</packaging>
                    <file>${basedir}/lib/desktop-game-engine.jar</file>
                    <generatePom>true</generatePom>
                </configuration>
            </plugin>
           
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.10.1</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>

            <!-- Run java-fx app over Maven -->
            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>0.0.8</version>
                <configuration>
                    <mainClass>com.javarush.games.racer.RacerGame</mainClass>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>3.6.0</version>
                <configuration>
                    <outputDirectory>
                        ${basedir}/lib/
                    </outputDirectory>
                    <overWriteReleases>true</overWriteReleases>
                    <overWriteSnapshots>true</overWriteSnapshots>
                    <overWriteIfNewer>true</overWriteIfNewer>
                </configuration>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

我尝试过添加各种pom块、关联资源到插件、修改插件构建步骤及配置,但都没解决问题。

问题分析与修复步骤

你的pom.xml存在几个关键问题,导致主类无法被正确加载:

1. 错误的资源配置

你把源码目录下的.class文件和lib目录的jar当成资源处理,这完全错误。Maven默认会将编译后的class文件放在target/classes,并自动打包进Jar,无需额外配置。

修复:删除<build>下的整个<resources>节点。

2. 重复的编译插件配置

你配置了两个版本的maven-compiler-plugin(3.11.0和3.10.1),会导致编译行为冲突,影响class文件生成。

修复:合并成一个配置,保留最新稳定版:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.11.0</version>
    <configuration>
        <source>18</source>
        <target>18</target>
        <encoding>UTF-8</encoding>
    </configuration>
</plugin>

3. Jar插件配置冗余且版本过旧

你使用的maven-jar-plugin是2.0旧版本,还保留了多余的executions块,同时manifestFile指定了外部文件,可能覆盖正确配置。

修复:简化配置,升级到最新稳定版,明确指定类路径:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>3.3.0</version>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <classpathPrefix>lib/</classpathPrefix>
                <mainClass>org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader</mainClass>
            </manifest>
            <manifestEntries>
                <Rsrc-Main-Class>com.javarush.games.racer.RacerGame</Rsrc-Main-Class>
                <Rsrc-Class-Path>./ desktop-game-engine.jar commons-lang3-3.12.0.jar javafx-controls-18.0.1.jar</Rsrc-Class-Path>
            </manifestEntries>
        </archive>
    </configuration>
</plugin>

4. 系统依赖处理不当

desktop-game-engine使用system scope,容易导致依赖无法被正确包含到类路径。

修复:先将该jar安装到本地Maven仓库,执行以下命令:

mvn install:install-file -Dfile=lib/desktop-game-engine.jar -DgroupId=com.javarush -DartifactId=desktop-game-engine -Dversion=1.0 -Dpackaging=jar

然后修改依赖为普通依赖,去掉system和systemPath:

<dependency>
    <groupId>com.javarush</groupId>
    <artifactId>desktop-game-engine</artifactId>
    <version>1.0</version>
</dependency>

5. 验证主类编译结果

执行mvn clean compile,检查target/classes/com/javarush/games/racer/目录下是否存在RacerGame.class文件,确保主类被正确编译。

6. 重新构建运行

执行mvn clean package,然后运行生成的Jar:

java -jar target/project-maven-1.0.jar

内容的提问来源于stack exchange,提问作者SteLax

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 15:55:54