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

niv*_*000 5 java selenium maven selenium-webdriver

我在pom.xml中添加了最新的Selenium依赖项

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.7.1</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

我使用pom.xml在目录内运行了mvn clean install,并且根据Selenium文档,我还在应用程序类中导入了正确的类。

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试运行主方法时,出现以下错误

Exception in thread "main" java.lang.NoClassDefFoundError: 
org/openqa/selenium/WebDriver
Run Code Online (Sandbox Code Playgroud)

我查看我的〜/ .m2 / repository文件夹,但没有看到openqa文件夹,但看到了seleniumhq文件夹。

为什么maven不安装openqa文件夹,为什么文档说要从org.openqa导入...当我的jar存储库中不存在该文件时。我很困惑,我只想在本地存储库中成功导入Selenium Webdriver。

inv*_*bl3 5

首先,正确检查您的程序是否具有所有重要的依赖项。
其次,我在运行maven项目时遇到类似的错误:

Caused by: java.lang.NoClassDefFoundError: org/openqa/selenium/JavascriptExecutor
Run Code Online (Sandbox Code Playgroud)

这个问题是因为插件不合适,因为我测试了不同版本的Selenium,但它对我没有帮助。

所以当我改变时maven-jar-plugin

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
 <configuration>
   <archive>
        <manifest>
             <addClasspath>true</addClasspath>
             <classpathPrefix>lib/</classpathPrefix>
             <mainClass>your_main_class</mainClass>
        </manifest>
   </archive>
 </configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)

插上maven-shade-plugin

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.0.0</version>
    <executions>
       <execution>
            <phase>package</phase>
            <goals>
               <goal>shade</goal>
            </goals>
            <configuration>
                 <transformers>
                    <transformer 
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>your_main_class</mainClass>
                    </transformer>
                 </transformers>
             </configuration>
       </execution>
    </executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)

问题就消失了。您可以在这里找到插件之间的区别。


此外,有时我们会升级我们的库,即使方法名称相同。由于版本不同,当一个库与此类升级不兼容时,我们会在运行时收到NoClassDefFoundErrorNoSuchMethodError错误。

Java 构建工具和 IDE 还可以生成依赖关系报告,告诉您哪些库依赖于该 JAR。大多数情况下,识别并升级依赖于旧 JAR 的库可以解决该问题。


总结一下:

  • 如果 Selenium 包含所有依赖项,请尝试更改其版本;
  • 如果没有,请尝试添加必要的依赖项;
  • 尝试检查 maven 文件夹是否有特定错误;
  • 如果以上没有帮助,请尝试使用插件。


小智 5

在 Eclipse IDE 中遇到此错误。在 Eclipse 中,转到项目属性,然后在 Java 构建路径中,只需在类路径而不是模块路径中添加 selenium jar。然后在顶部的“项目”选项卡下执行“清理”以删除早期的 buiid,然后执行“运行”。


How*_*hih 0

  • org.openqa.seleniumselenium-api-{version}.jar是文件夹下的包seleniumhq\selenium\selenium-api
  • org.openqa.selenium.firefoxselenium-firefox-driver-{version}.jar是文件夹下的包seleniumhq\selenium\selenium-firefox-driver

所以没有 openqa 文件夹,它只是 seleniumhq 文件夹下的包名称,您应该检查这些 jar。

如果没有项目结构和代码细节,很难说是什么导致了NoClassDefFoundError异常。异常与 不同ClassNotFoundException。也许这个答案/sf/answers/402989261/会有所帮助。