遇到错误:java.lang.NoClassDefFoundError:com/google/common/collect/ImmutableList$Builder

2024-02-07

我是使用 EclipseIDE 和 TestNG 来使用 Selenium WebDriver 的新手。我目前正在 Eclipse 中通过 TestNG 运行此示例代码:

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.util.List;

public class CheesecakeFactory {

    FirefoxDriver driver;

    @BeforeTest
    public void startDriver() {
        driver = new FirefoxDriver();
    }

    @AfterTest
    public void stopDriver() {
        driver.close();
    }

    @Test
    public void listCheesecakes() {
        driver.get("http://www.thecheesecakefactory.com/");
        driver.findElement(By.linkText("Menu")).click();
        driver.findElement(By.linkText("Cheesecake")).click();
        List<WebElement> cheesecakes = driver.findElements(By.xpath("id('leftNav_levelTwo')//li"));

        System.out.println(cheesecakes.size() + " cheesecakes:");
        for (int i=0; i<cheesecakes.size(); i++) {
            System.out.println(i+1 + ". " + cheesecakes.get(i).getText());
        }
    }

}

但 Eclipse 返回这个:

[TestNG] Running:
  C:\Users\ryan\AppData\Local\Temp\testng-eclipse--616826937\testng-customsuite.xml

FAILED CONFIGURATION: @BeforeTest startDriver
java.lang.NoClassDefFoundError: com/google/common/collect/ImmutableList$Builder
    at org.openqa.selenium.os.WindowsUtils.getPathsInProgramFiles(WindowsUtils.java:275)
    at org.openqa.selenium.firefox.internal.Executable.locateFirefoxBinaryFromPlatform(Executable.java:148)
    at org.openqa.selenium.firefox.internal.Executable.<clinit>(Executable.java:25)
    at org.openqa.selenium.firefox.FirefoxBinary.<init>(FirefoxBinary.java:60)
    at org.openqa.selenium.firefox.FirefoxBinary.<init>(FirefoxBinary.java:56)
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:78)
    at CheesecakeFactory.startDriver(CheesecakeFactory.java:16)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
    at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138)
    at org.testng.TestRunner.beforeRun(TestRunner.java:641)
    at org.testng.TestRunner.run(TestRunner.java:609)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
    at org.testng.SuiteRunner.run(SuiteRunner.java:240)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1197)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1122)
    at org.testng.TestNG.run(TestNG.java:1030)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

我不明白为什么我会收到此错误。我做了以下事情:

  1. 添加了 guava-12.0.jar 文件(以及 Selenium-2.25.0 Webdriver 中的其他 jar 文件)作为 Eclipse 中的外部 jar 文件。 (此 jar 文件包含 ImmutableList$Builder 类)

  2. 在CLASSPATH中添加该jar文件的路径(环境变量>系统变量)

我错过了什么吗?任何帮助是极大的赞赏。


我猜你正在使用 selenium-java-2.25.0.jar。你应该使用selenium-server-standalone-2.25.0.jar,它将处理所有依赖项(即所需的 jar 文件)。

另外,如果 jar 文件添加到 Eclipse 中,则不需要显式定义环境变量,除非您在 Eclipse 外部运行测试。

希望这可以帮助... :)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

遇到错误:java.lang.NoClassDefFoundError:com/google/common/collect/ImmutableList$Builder 的相关文章

随机推荐