NoClassDefFound error during Selenium WebDriver -

2019-08-19 04:25发布

问题:

I'm just starting out writing out Java tests to automate native Android apps using Appium and JUnit. Getting a NoClassDefFound error even if the required jars are on class path in Eclipse GUI. I've narrowed it down to this line :

driver = new RemoteWebDriver(new URL("appium typical server url here"), capabilities);

The following jars are in the Java build path in Eclipse under Libraries: java-client-1.5.0.jar, selenium-java-2.42.2.jar, selenium-java-2.42.2-srcs.jar, along with JUnit

Here is my complete code:

package com.chinmay.automation;

import java.io.File;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

    public class ChessTest {

    private WebDriver driver=null;

    @Before

    public void setUp() throws Exception{

        File appDir= new File("/Users/csathe/Desktop/Java");
        File app = new File(appDir, "ChessFree.apk");
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
        capabilities.setCapability(CapabilityType.VERSION, "4.3");
        capabilities.setCapability(CapabilityType.PLATFORM, "Mac");
        capabilities.setCapability("device", "android");
        capabilities.setCapability("app-package", "uk.co.aifactory.chessfree");
        capabilities.setCapability("app-activity", "ChessFreeActivity");
        capabilities.setCapability("app", app.getAbsolutePath());

        driver= new RemoteWebDriver(new URL("//appium typical server url here"), capabilities);


    }

    @Test

    public void testFirst(){

        // do something

    }
    @After

    public void tearDown(){

        // driver.quit();
       }

    }

If I run this using JUnit with that line commented, it runs fine (does nothing however).

The stacktrace with the line:

java.lang.NoClassDefFoundError: com/google/common/base/Function
at com.chinmay.automation.ChessTest.setUp(ChessTest.java:39)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)

at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at   
Caused by: java.lang.ClassNotFoundException: com.google.common.base.Function
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 25 more

Running this one class outside JUnit leads to the same error.

回答1:

This error clearly indicates that any jar dependency is missing from the build path.

Please add all the jars available here to the class path :

http://selenium-release.storage.googleapis.com/2.42/selenium-java-2.42.2.zip

OR

Download the Selenium-server-standalone.jar from here and add to the class path:

http://selenium-release.storage.googleapis.com/2.42/selenium-server-standalone-2.42.2.jar



回答2:

You can retry by selecting mentioned JAR files under Order and Export tab. Can be accessed in the way mentioned below:

Right click on the project -> Build Path -> Configure Build Path -> Order and Export -> Select required jars (e.g. Selenium,java etc.) and click on OK.

Lemme know if this works for you!