简体   繁体   中英

java.lang.NoClassDefFoundError: org/apache/commons/exec/Executor

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

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 eg 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-xyz Java Client use selenium-server-standalone-xyzjar 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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM