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" file too but it is not working. Do I need to add any other jar file ??
package cross_browser;
import org.testng.annotations.Test;
import org.testng.annotations.Parameters;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
public class NewTest {
WebDriver driver;
String url;
@BeforeClass
@Parameters({"browser"})
public void beforeTest(String browser) {
url = "https://www.facebook.com";
if (browser.equalsIgnoreCase("chrome"))
{
System.setProperty("webdriver.gecko.driver", "C:\\Users\\Prateek\\Desktop\\Drivers\\geckodriver.exe");
driver = new FirefoxDriver();
}
if (browser.equalsIgnoreCase("firefox"))
{
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Prateek\\Desktop\\Drivers\\chromedriver.exe");
driver = new ChromeDriver();
}
driver.manage().window().maximize();
}
@Test
public void Test() {
driver.get(url);
driver.findElement(By.id("email")).sendKeys("Prateek");
driver.findElement(By.id("password")).sendKeys("Prateek");
driver.findElement(By.id("login")).click();
}
@AfterClass
public void afterTest() {
driver.quit();
}
}
XML File:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name = "CrossBrowser Testing" parallel = "tests" thread-count ="2">
<test name = "Chrome Testing">
<parameter name ="browser" value = "chrome"></parameter>
<classes>
<class name = "cross_browser.NewTest"></class>
</classes>
</test>
<test name = "firefox Testing">
<parameter name ="browser" value = "firefox"></parameter>
<classes>
<class name = "cross_browser.NewTest"></class>
</classes>
</test>
</suite>
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
Try adding this dependency, it worked for me
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-exec</artifactId>
<version>1.3</version>
</dependency>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.