Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java.lang.NoClassDefFoundError:org/apache/commons/exec/Executor_Java_Selenium - Fatal编程技术网

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

java.lang.NoClassDefFoundError:org/apache/commons/exec/Executor,java,selenium,Java,Selenium,我试图执行以下代码,但在运行时出现错误“java.lang.NoClassDefFoundError:org/apache/commons/exec/Executor”。我也添加了“commonexecjar”文件,但它不起作用。 我需要添加任何其他jar文件吗 package cross_browser; import org.testng.annotations.Test; import org.testng.annotations.Parameters; import org.openqa

我试图执行以下代码,但在运行时出现错误“java.lang.NoClassDefFoundError:org/apache/commons/exec/Executor”。我也添加了“commonexecjar”文件,但它不起作用。 我需要添加任何其他jar文件吗

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文件:

<!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
在Java中,当Java虚拟机无法在运行时找到编译时可用的特定类时,会发生该错误。例如,如果我们解析了一个类的方法调用或访问了一个类的任何静态成员,而该类在运行时不可用,那么JVM将抛出NoClassDefFoundError

您看到的错误是:

Exception in thread "main" java.lang.NoClassDefFoundError: 
org/apache/commons/exec/Executor
这清楚地表明Selenium正试图在运行时从
org/apache/commons/exec/Executor
解析一个特定的类,该类不可访问/不可用

出了什么问题: 您提到过添加公共Exec jar,但相关的类或方法似乎是在编译时从一个源解析的,而在运行时不可用

如果存在多个源来通过JDK/Maven/Gradle解析类和方法,并且使用新JAR文件升级时会发生此错误

解决方案: 以下是解决NoClassDefFoundError的几个步骤:

  • 如果在Java项目中使用Selenium JAR,则仅在Java构建路径中添加所需的外部JAR,并删除未使用/不需要的JAR
  • 如果使用构建工具,例如Maven或Gradle,Java构建路径中删除所有外部jar。Maven或Gradle将下载并解析所有必需的依赖项
  • 如果使用Selenium jar,请使用而不是使用Selenium-java-x.y.zjava客户端使用Selenium-server-standalone-x.y.z.jar将所有必需的依赖项捆绑在一起
  • 在使用Maven的情况下,可以使用
    工具定期清除操作系统的杂务
  • 重新启动系统
  • 如果您正在执行一个Maven项目,请始终执行
    Maven clean
    Maven安装
    ,然后执行
    Maven测试

尝试添加此依赖项,它对我有效

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-exec</artifactId>
    <version>1.3</version>
</dependency>

org.apache.commons
下议院行政长官
1.3

TestNG安装一定有问题。您使用什么命令来执行?