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

含外部依赖库的Jar包启动失败问题求助

解决Maven打包Jar后无法找到主类的问题

你的核心问题是:直接运行Jar时,JVM找不到依赖的Selenium、Apache Poi类,进而导致主类加载失败(注释依赖后能运行,也佐证了这一点)。以下是几种实用的解决方法:

方法一:用Maven Shade插件打可执行胖Jar(最推荐)

Shade插件会把你的代码+所有依赖打包进同一个Jar,同时自动配置主类,直接就能运行。

  1. 在pom.xml中添加插件配置:
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.4.1</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <!-- 指定你的主类全路径 -->
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>com.example.calcoloeffemeridi.Launcher</mainClass>
                            </transformer>
                        </transformers>
                        <!-- 解决依赖包的签名文件冲突 -->
                        <filters>
                            <filter>
                                <artifact>*:*</artifact>
                                <excludes>
                                    <exclude>META-INF/*.SF</exclude>
                                    <exclude>META-INF/*.DSA</exclude>
                                    <exclude>META-INF/*.RSA</exclude>
                                </excludes>
                            </filter>
                        </filters>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
  1. 执行打包命令:
mvn clean package

打包完成后,target目录下会生成两个Jar文件,运行带-shaded后缀的那个即可:

java -jar target/你的项目名-shaded.jar

方法二:用Maven Assembly插件打包含依赖的Jar

这个插件会生成一个包含所有依赖的可执行Jar,配置更简单。

  1. 在pom.xml中添加插件配置:
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.6.0</version>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <mainClass>com.example.calcoloeffemeridi.Launcher</mainClass>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
  1. 执行打包命令:
mvn clean package

运行target目录下带-jar-with-dependencies后缀的Jar即可。

方法三:手动指定类路径运行瘦Jar(适合需单独管理依赖的场景)

如果不想打胖Jar,可以把依赖包单独放在lib目录,运行时指定类路径。

  1. 在pom.xml中添加配置:
<build>
    <plugins>
        <!-- 配置主类信息 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.3.0</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>com.example.calcoloeffemeridi.Launcher</mainClass>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
        <!-- 自动复制依赖到lib目录 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>3.5.0</version>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/lib</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
  1. 打包后,将target下的Jar和lib文件夹放在同一目录,运行:
java -jar 你的项目名.jar

或者手动指定类路径:

java -cp "你的项目名.jar;lib/*" com.example.calcoloeffemeridi.Launcher

排查注意事项

  • 确认pom.xml中的主类全路径无拼写错误(包名、类名大小写要完全匹配)。
  • 打包后打开Jar包,检查META-INF/MANIFEST.MF文件,确保Main-Class字段存在且正确。
  • 保证Selenium、Apache Poi的依赖范围是compile(默认就是,无需额外修改)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 01:52:15