Encountering error: java.lang.NoClassDefFoundError: com/google/common/collect/ImmutableList$Builder

萝らか妹 提交于 2020-01-04 02:46:05

问题


I'm new to Selenium WebDriver using EclipseIDE with TestNG. I'm currently running this sample code in Eclipse via 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());
        }
    }

}

But Eclipse returns this:

[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)

I don't understand why I'm getting this error. I've done the following:

  1. Added the guava-12.0.jar file (along with the other jar files in the Selenium-2.25.0 webdriver) as an external jar file in Eclipse. (This jar file contains the ImmutableList$Builder class)

  2. Added the path of this jar file in the CLASSPATH (Environment Variables>System Variables)

Am I missing something? Any help is greatly appreciated.


回答1:


I guess you are using selenium-java-2.25.0.jar. You should rather use selenium-server-standalone-2.25.0.jar, it will take care of all the dependencies (i.e. required jar files).

Also you dont need to explicitly define the environment variables if the jar files are added in the Eclipse, unless you are running the test outside from eclipse.

Hope this helps... :)



来源:https://stackoverflow.com/questions/12237153/encountering-error-java-lang-noclassdeffounderror-com-google-common-collect-im

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!