4

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>
1
  • Must be something wrong with the TestNG installation. What command are you using to execute ? Commented Apr 3, 2018 at 4:40

3 Answers 3

3

Try adding this dependency, it worked for me

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-exec</artifactId>
    <version>1.3</version>
</dependency>
Sign up to request clarification or add additional context in comments.

Comments

2

NoClassDefFoundError

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.

What went wrong :

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.

Solution :

Here are a few steps to solve NoClassDefFoundError :

  • If using Selenium JARs within a Java Project add only required External JARs within the Java Build Path and remove the unused/unwanted one.
  • Incase using a Build Tool e.g. Maven or Gradle, remove all the External JARs from the Java Build Path. Maven or Gradle will download and resolve all the required dependencies.
  • If using Selenium JARs, either use rather then using selenium-java-x.y.z Java Client use selenium-server-standalone-x.y.z.jar instead which bundles all the required dependencies together.
  • Incase 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> from pom.xml
  • Clean you Project Workspace within your IDE periodically only to build your project with required dependencies.
  • Use CCleaner tool to wipe away the OS chores periodically.
  • take a System Reboot.
  • Incase you are executing a Maven Project always do maven clean, maven install and then maven test

Comments

0

I got the same error, i removed the jar files from class path and added again. and my error doesnt appear again, my code ran and got the result

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.