gpt4 book ai didi

java - 线程 "main"java.lang.NoClassDefFoundError : org/openqa/selenium/WebDriver 中出现异常

转载 作者:行者123 更新时间:2023-12-01 16:23:50 50 4
gpt4 key购买 nike

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

<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.7.1</version>
</dependency>

我跑了 mvn 干净安装在我的 pom.xml 目录中,我还根据 Selenium 文档在我的应用程序类中导入了正确的类

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

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

Exception in thread "main" java.lang.NoClassDefFoundError: 
org/openqa/selenium/WebDriver

我查看 ~/.m2/repository 文件夹,没有看到 openqa 文件夹,而是看到了 seleniumhq 文件夹。

为什么 maven 没有安装 openqa 文件夹,为什么文档说要从 org.openqa 导入...而我的 jar 存储库中从来不存在这个文件夹。我很困惑,我只想能够成功导入 selenium Webdriver,同时将其保存在我的本地存储库中。

最佳答案

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

Caused by: java.lang.NoClassDefFoundError: org/openqa/selenium/JavascriptExecutor

这个问题是因为插件不合适,因为我测试了不同版本的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>

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>

问题已经解决了。插件的区别你可以找到here .

<小时/>

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

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

<小时/>

总结一下:

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

关于java - 线程 "main"java.lang.NoClassDefFoundError : org/openqa/selenium/WebDriver 中出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62199619/

50 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com