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

为何Selenium在IntelliJ中正常运行,命令行执行Jar却失败?

解决Maven打包Selenium项目后运行Jar缺失WebDriver类的问题

核心原因

IntelliJ运行时会自动加载Maven依赖库中的所有依赖,但默认Maven Jar插件只会打包你的项目代码,不会包含Selenium、Guava这类第三方依赖,导致终端运行Jar时找不到org.openqa.selenium.WebDriver这类类。下面是实测有效的解决方案:


步骤1:确认依赖Scope配置

首先检查Selenium和Guava的依赖Scope,确保是默认的compile(不要设为provided或test,否则打包时会被排除),同时保持Guava的排除配置:

<dependencies>
    <!-- Selenium依赖,排除自带的Guava -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>4.7.1</version>
        <exclusions>
            <exclusion>
                <groupId>com.google.guava</groupId>
                <artifactId>guava</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <!-- 显式引入指定版本的Guava -->
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>31.1-jre</version> <!-- 推荐用jre适配版本,减少额外依赖问题 -->
    </dependency>
</dependencies>

步骤2:用maven-shade-plugin打包可执行Jar

这是最可靠的方式,它会把所有依赖合并到一个Jar里,解决类缺失问题。在pom.xml的<build>标签下添加以下配置:

<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>
                        <!-- 指定你的主类全路径,比如 com.example.SeleniumMain -->
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>你的主类全限定名</mainClass>
                            </transformer>
                        </transformers>
                        <!-- 处理Guava的文件合并冲突 -->
                        <filters>
                            <filter>
                                <artifact>com.google.guava:guava</artifact>
                                <includes>
                                    <include>**/*</include>
                                </includes>
                            </filter>
                        </filters>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

打包与运行

  1. 执行命令完成打包:mvn clean package
  2. 到项目的target目录,找到带-shaded后缀的Jar文件(比如your-project-1.0-SNAPSHOT-shaded.jar)
  3. 终端运行该Jar:java -jar target/your-project-1.0-SNAPSHOT-shaded.jar

备选方案:复制依赖后指定类路径运行

如果不想合并Jar,可以用maven-dependency-plugin把依赖复制到单独目录,然后运行时指定类路径:

<build>
    <plugins>
        <!-- 复制依赖到target/lib目录 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>3.6.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>
        <!-- 生成带主类信息的普通Jar -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.3.0</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>你的主类全限定名</mainClass>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

运行命令(Windows用;代替:):

java -cp target/your-project.jar:target/lib/* 你的主类全限定名

额外检查点

  1. 用mvn dependency:tree查看依赖树,确认Guava只有31.1版本被引入,没有其他冲突版本
  2. 确保终端使用的Maven和IntelliJ配置的是同一个,避免依赖仓库不一致
  3. 主类必须在src/main/java目录下,不要放在src/test/java中

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 19:01:52