I am trying to execute the below code but getting error \"java.lang.NoClassDefFoundError: org/apache/commons/exec/Executor\" while running. I have added \"Common-Exec jar\"
Try adding this dependency, it worked for me
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-exec</artifactId>
<version>1.3</version>
</dependency>
NoClassDefFoundError
in Java occurs when Java Virtual Machine is 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 JVM will throw NoClassDefFoundError.
The error you are seeing is :
Exception in thread "main" java.lang.NoClassDefFoundError:
org/apache/commons/exec/Executor
This clearly indicates that Selenium is trying to resolve a particular class at runtime from org/apache/commons/exec/Executor
which is not accessible/available.
You have mentioned of adding Common-Exec jar but it seems that the related Class or Methods were resolved from one source during Compile Time which was not available during Run Time.
This error occurs if there are presence of multiple sources to resolve the Classes and Methods through JDK/Maven/Gradle and this situation happens when upgrading with new JAR files.
Here are a few steps to solve NoClassDefFoundError
:
<artifactId>selenium-java</artifactId>
or <artifactId>selenium-server</artifactId>
. Avoid using both at the same time.<dependency>
from pom.xmlmaven clean
, maven install
and then maven test