【问题标题】:Why do I get "Exception in thread "main" java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver"?为什么我会收到“线程“主”java.lang.NoClassDefFoundError:org/openqa/selenium/WebDriver 中的异常”?
【发布时间】:2021-03-04 18:23:19
【问题描述】:

我在这里尝试了针对其他类似问题的一些建议,但未能解决问题。 尝试通过以下方式执行 jar 时出现此错误:

java -jar AutoHotRouter-1.0.jar

鉴于以下 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>com.ttt</groupId>
    <artifactId>AutoHotRouter</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>

    <name>AutoHotRouter</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.11.2</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-api</artifactId>
            <version>2.53.1</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-chrome-driver</artifactId>
            <version>2.53.1</version>
        </dependency>
    </dependencies>

    <build>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.1.0</version>
                </plugin>
                <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.22.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-site-plugin</artifactId>
                    <version>3.7.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-project-info-reports-plugin</artifactId>
                    <version>3.0.0</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

首先我添加了:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <outputDirectory>
                    ${project.build.directory}/libs
                </outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <classpathPrefix>libs/</classpathPrefix>
                <mainClass>com.ttt.App</mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>

我用

构建了这个jar
mvn clean install

然后开始

java -jar AutoHotRouter.jar 

我明白了:

线程“主”java.lang.NoClassDefFoundError 中的异常: org/openqa/selenium/WebDriver

然后我删除了以前的插件并首先添加了 maven-assembly-plugin,因为它对我不起作用,所以我用 maven-shade-plugin 替换它:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>3.3.0</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>com.ttt.App</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </execution>
    </executions>
</plugin>

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <shadedArtifactAttached>true</shadedArtifactAttached>
                <transformers>
                    <transformer implementation=
                                         "org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>com.ttt.App</mainClass>
                    </transformer>
                </transformers>
            </configuration>
        </execution>
    </executions>
</plugin>

之后

mvn clean install 

然后

java -jar AutoHotRouter-1.0.jar

我明白了:

没有主要的清单属性

我也试过

mvn clean compile assembly:single

与 maven-assembly-plugin。

然后我得到:

读取程序集时出错:未找到程序集描述符。

谢谢!

【问题讨论】:

    标签: maven selenium-webdriver maven-3


    【解决方案1】:

    这只是一个假设,因为到目前为止 pom 看起来不错。有点可疑的部分是jar插件:

    <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-jar-plugin</artifactId>
          <version>2.1</version>
          <configuration>
          ...
    

    看起来您稍后尝试使用java -jar 执行创建的 jar 文件?在这种情况下,您在 pom 中定义的所有依赖项都将丢失。使用 dependency-plugin 收集依赖项 jar 文件并在运行 jar 时使用类路径选项,或者使用 shade-plugin 创建一个包含您的类和依赖项的 uber-jar。

    使用你的 pom 允许我启动 Chrome,所以依赖项看起来不错。所以我认为您启动它的方式会导致该异常。

    更新:由于您使用此设置来自动化 chrome,因此 shade 插件似乎是最好的方法。我可以在 src/main/java 中使用这个 pom 和主类启动 chrome

    <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>org.example</groupId>
    <artifactId>HotRouter</artifactId>
    <version>1.0-SNAPSHOT</version>
    
    <name>HotRouter</name>
    
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>
    
    <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.14.0</version>
        </dependency>
    </dependencies>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.4</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>org.example.StartMain</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    

    主类

    package org.example;
    
    import org.openqa.selenium.chrome.ChromeDriver;
    
    public class StartMain {
    
      public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "C:/Program Files/Google/Chrome/Application/chrome.exe");
    
        ChromeDriver driver = new ChromeDriver();
      }
    }
    

    然后mvn packagejava -jar ./target/HotRouter-1.0-SNAPSHOT.jar 打开 chrome。您的配置几乎相同(我认为只有阴影插件的阶段配置丢失)

    在 pom 的 &lt;plugins&gt; 部分中包含 shade 插件很重要,因为它不是 the default jar life-cycle 的一部分。 &lt;pluginManagement&gt; 部分只是用于配置版本和配置的默认值。见pom reference。因此,如果仅在该部分中,将不会自动启用其他插件。

    【讨论】:

    • 谢谢@wemu。我添加了阴影插件:org.apache.maven.pluginsmaven-shade-plugin3.2.4packageshade插件>。我仍然得到同样的错误。我应该以不同的方式配置插件吗?
    • 这完全取决于您如何运行此构建。您的 selenium 依赖项在 pom.xml 中没有常见的测试范围。 shade 插件用于将您的生产源打包到一个可执行的 jar 中。它不包括测试源。所以这也取决于你放置测试的位置(通常是 src/test/maven,但通常不包含在 shade 插件中)。 Maven构建是否会导致相同的异常?还是您实际上是在尝试通过可执行 jar 运行它?这对于运行测试也不是很常见。
    • 谢谢@wemu。实际上,我只使用 Selenium 来自动化 Chrome,而不是用于测试。 Maven (mvn clean install) 成功。 java -jar aaa.jar 给出错误...
    • 我在问题中添加了更多信息
    • “mvn package”是否创建了 uber-jar?在目标文件夹中应该有一个更大的文件夹和一个“original-.jar”文件夹。阴影插件的执行可能还需要目标部分附近的 package。您也不需要为 jar-plugin 配置主类,因为 shade 插件将为 uber-jar 执行此操作。
    猜你喜欢
    • 2016-07-06
    • 2018-05-29
    • 1970-01-01
    • 2017-12-21
    • 2019-03-09
    • 1970-01-01
    • 2014-11-25
    • 2019-12-20
    相关资源
    最近更新 更多