线程“main”中的异常 java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47823506/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Exception in thread "main" java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver
提问by nivo1000
I have added the most updated Selenium dependency in my pom.xml
我在 pom.xml 中添加了最新的 Selenium 依赖项
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.7.1</version>
</dependency>
I ran mvn clean install inside the directory with my pom.xml and I have also imported the correct classes in my app class as per the Selenium documentation
我使用 pom.xml 在目录中运行了 mvn clean install 并且我还根据 Selenium 文档在我的应用程序类中导入了正确的类
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
However when i try and run my main method, I get the following error
但是,当我尝试运行我的主要方法时,出现以下错误
Exception in thread "main" java.lang.NoClassDefFoundError:
org/openqa/selenium/WebDriver
I look in my ~/.m2/repository folder and I don't see an openqa folder but instead I see a seleniumhq folder.
我查看了 ~/.m2/repository 文件夹,但没有看到 openqa 文件夹,而是看到了 seleniumhq 文件夹。
Why didn't maven install the openqa folder, and why does the documentation say to import from org.openqa... when that never exist in my jar repository. I'm very confused, I just want to be able to import selenium Webdriver successfully while having it in my local repository.
为什么 maven 不安装 openqa 文件夹,为什么文档说要从 org.openqa 导入...当我的 jar 存储库中不存在时。我很困惑,我只想在我的本地存储库中成功导入 selenium Webdriver。
回答by invzbl3
Firstly, check properly if you have all important dependencies for your program.
Secondly, I had similar error while running maven project:
首先,正确检查您的程序是否具有所有重要的依赖项。
其次,我在运行 Maven 项目时遇到了类似的错误:
Caused by: java.lang.NoClassDefFoundError: org/openqa/selenium/JavascriptExecutor
And this problem was because of inappropriate plugin, because I tested different versions of Selenium and it didn't help me.
而这个问题是因为不合适的插件,因为我测试了不同版本的 Selenium 并没有帮助我。
So when I changed maven-jar-plugin
:
所以当我改变时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>
to maven-shade-plugin
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>
The issue was gone. The difference between plugins you can find here.
问题消失了。您可以在此处找到插件之间的区别。
In addition, sometimes we upgrade our libraries even with same method name. Due this different in version, we get NoClassDefFoundErroror NoSuchMethodErrorat runtime when one library was not compatible with such an upgrade.
此外,有时我们甚至使用相同的方法名称升级我们的库。由于版本不同,当一个库与此类升级不兼容时,我们会在运行时收到NoClassDefFoundError或NoSuchMethodError。
Java build tools and IDEs can also produce dependency reports that tell you which libraries depend on that JAR. Mostly, identifying and upgrading the library that depends on the older JAR resolve the issue.
Java 构建工具和 IDE 还可以生成依赖关系报告,告诉您哪些库依赖于该 JAR。大多数情况下,识别和升级依赖于旧 JAR 的库可以解决该问题。
To summarize:
总结一下:
- try to change versions of Selenium, if it contains all dependencies;
- try to add necessary dependencies if you don't have it;
- try to check folder of maven if it has or not what says specific error;
- try to play with plugins if nothing helps above.
- 尝试更改 Selenium 的版本,如果它包含所有依赖项;
- 如果没有,请尝试添加必要的依赖项;
- 尝试检查 maven 文件夹是否有特定错误;
- 如果上面没有任何帮助,请尝试使用插件。
回答by DebanjanB
NoClassDefFoundError
NoClassDefFoundError
NoClassDefFoundError
in Java occurs when Java Virtual Machineis not able to find a particular class at runtime which was available at compile time. For example, if we have resolved a method call from a class or accessing any static member of a Class and that Class is not available during run-time then JVMwill throw NoClassDefFoundError
.
NoClassDefFoundError
在 Java 中,当Java 虚拟机无法在运行时找到在编译时可用的特定类时,就会发生这种情况。例如,如果我们解决了来自类的方法调用或访问类的任何静态成员,并且该类在运行时不可用,则JVM将抛出NoClassDefFoundError
.
The error you are seeing is :
您看到的错误是:
Exception in thread "main" java.lang.NoClassDefFoundError:
org/openqa/selenium/WebDriver
This clearly indicates that Seleniumis trying to resolve the particular class at runtime from org/openqa/selenium/WebDriver
which is no more available.
这清楚地表明Selenium正在尝试在运行时解析org/openqa/selenium/WebDriver
不再可用的特定类。
As you mentioned of looking into ~/.m2/repository
folder, the maven folder structure for Selenium v3.7.1(on Windows) is as follows :
正如您提到的查看~/.m2/repository
文件夹,Selenium v3.7.1(在 Windows 上)的 maven 文件夹结构如下:
C:\Users\<user_name>\.m2\repository\org\seleniumhq\selenium\selenium-java.7.1
So when you see a seleniumhq
folder, it is pretty much expected.
所以当你看到一个seleniumhq
文件夹时,它几乎是在意料之中的。
What went wrong :
什么地方出了错 :
From all the above mentioned points it's clear that the related Classor Methodswere resolved from one source Compile Timewhich was not available during Run Time.
从上面提到的所有点可以清楚地看出,相关的类或方法是从一个源Compile Time解析的,在Run Time期间不可用。
This situation occurs if there are presence of multiple sources to resolve the Classes and Methods through JDK/ Maven/ Gradle.
如果存在通过JDK/ Maven/ Gradle解析类和方法的多个源,则会出现这种情况。
Solution :
解决方案 :
Here are a few steps to solve NoClassDefFoundError:
以下是解决NoClassDefFoundError的几个步骤:
- While using a Build Tool e.g. Mavenor Gradle, removeall the External JARsfrom the Java Build Path. Mavenor Gradlewill download and resolve all the required dependencies.
- If using Selenium JARswithin a Java Projectadd only required External JARswithin the Java Build Pathand remove the unused one.
- While using Maven, either use
<artifactId>selenium-java</artifactId>
or<artifactId>selenium-server</artifactId>
. Avoid using both at the same time. - Remove the unwanted other
<dependency>
frompom.xml
- Clean you Project Workspacwithin your IDE periodically only to build your project with required dependencies.
- Use CCleanetool to wipe away the OS chores periodically.
- While you execute a Maven Projectalways do
maven clean
,maven install
and thenmaven test
.
- 在使用构建工具(例如Maven或Gradle)时,从Java 构建路径中删除所有外部 JAR。Maven或Gradle将下载并解决所有必需的依赖项。
- 如果在Java 项目中使用Selenium JAR,则仅在Java 构建路径中添加所需的外部 JAR,并删除未使用的。
- 使用Maven 时,请使用
<artifactId>selenium-java</artifactId>
或<artifactId>selenium-server</artifactId>
。避免同时使用两者。 - 删除不需要的其他
<dependency>
从pom.xml
- 定期清理IDE 中的Project Workspac,仅用于构建具有所需依赖项的项目。
- 使用CCleane工具定期清除操作系统杂务。
- 当您执行Maven 项目时,始终执行
maven clean
,maven install
然后maven test
.
回答by Kostas Kaskavelis
What worked for me was to add this dependency to pom.xml:
对我有用的是将此依赖项添加到 pom.xml:
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>25.0-jre</version>
</dependency>
回答by HowieChih
org.openqa.selenium
is the package in theselenium-api-{version}.jar
under theseleniumhq\selenium\selenium-api
folder.org.openqa.selenium.firefox
is the package in theselenium-firefox-driver-{version}.jar
under theseleniumhq\selenium\selenium-firefox-driver
folder.
org.openqa.selenium
是文件夹selenium-api-{version}.jar
下的包seleniumhq\selenium\selenium-api
。org.openqa.selenium.firefox
是文件夹selenium-firefox-driver-{version}.jar
下的包seleniumhq\selenium\selenium-firefox-driver
。
So there is no openqa folder, it's just the package name under the seleniumhq folder, you should have a check into these jar.
所以没有openqa文件夹,它只是seleniumhq文件夹下的包名,你应该检查一下这些jar。
It's hard to say what caused NoClassDefFoundError
exception without project structure and code detail. The exception is not the same as ClassNotFoundException
. Maybe this answer https://stackoverflow.com/a/5756989/5374508would be helpful.
如果NoClassDefFoundError
没有项目结构和代码细节,很难说是什么导致了异常。例外与ClassNotFoundException
. 也许这个答案https://stackoverflow.com/a/5756989/5374508会有所帮助。
回答by Eugene S
Are you using an IDE or working from command line? In Eclipse for example you can force downloading all dependencies by right clicking on your project, going to Maven menu item and then selecting Update Project. Then check the "Force Update of Snapshots/Releases" checkbox.
您是使用 IDE 还是从命令行工作?例如,在 Eclipse 中,您可以通过右键单击您的项目,转到 Maven 菜单项,然后选择更新项目来强制下载所有依赖项。然后选中“强制更新快照/发布”复选框。
If you are opening from command line do:
如果您从命令行打开,请执行以下操作:
mvn clean install -U
from your project path.
从您的项目路径。