NoClassDefFoundError in Java: com/google/common/base/Function

JavaSelenium WebdriverRuntime Error

Java Problem Overview


When I executing the following code:

public static void main(String[] args) {
	try {
	    FirefoxDriver driver = new FirefoxDriver();
	    driver.get("http:www.yahoo.com");
    } catch (NoClassDefFoundError ex) {
        System.out.println("error: " + ex.getStackTrace());
	}
}

I'm facing the following error:

>error:[Ljava.lang.StackTraceElement;@80f4cb > >Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/base/Function


Could someone help me to find the solution or reason for this?

Java Solutions


Solution 1 - Java

I had the same problem, and finally I found that I forgot to add the selenium-server-standalone-version.jar. I had only added the client jar, selenium-java-version.jar.

Hope this helps.

Solution 2 - Java

A NoClassDefFoundError is thrown when the JRE can't find a class. In your case, it can't find the class com.google.common.base.Function, which you most probably did not add to your classpath.

EDIT

After downloading the following libraries:

and unzipping them and putting all JAR files in a folder called lib, the test class:

import org.openqa.selenium.firefox.FirefoxDriver;

public class Test {
    public static void main(String[] args) {
        try{
            FirefoxDriver driver = new FirefoxDriver();
            driver.get("http:www.yahoo.com");
        } catch(Exception e){
            e.printStackTrace();
        }
    }
}

ran without any problems.

You can compile and run the class as follows:

compile and run on Linux & Mac

javac -cp .:lib/* Test.java java -cp .:lib/* Test

compile and run on Windows

javac -cp .;lib/* Test.java java -cp .;lib/* Test

Solution 3 - Java

I encountered the same error and after the investigation, I found that library selenium-api 2.41.0 requires guava 15.0 but it was overridden by an older version so I declared guava 15.0 as a direct dependency by adding following configuration in pom.xml:

<dependency>
        <artifactId>guava</artifactId>
        <groupId>com.google.guava</groupId>
        <type>jar</type>
        <version>15.0</version>
</dependency>

Solution 4 - Java

you don't have the "google-collections" library on your classpath.

There are a number of ways to add libraries to your classpath, so please provide more info regarding how you are executing your program.

if from the command line, you can add libraries to the classpath via

java -classpath path/lib.jar ...

Solution 5 - Java

For me, in addition to selecting the jar - selenium-java-2.45.0.jar, I had to select all the jars in the "libs" folder under selenium root folder.

Solution 6 - Java

I got the same error, but it was resolved if you add the libraries of selenium (again if you haven't), if you are using INTELIJ

> project>projectStructure>Module>+>add the selenium jars (both from lib folder and outside ones.).

Same needs to be done for other IDE's as well, like eclipse.

Solution 7 - Java

It looks like you're trying to import some google code:

import com.google.common.base.Function;

And it's not finding it the class Function. Check to make sure all the required libraries are in your build path, and that you typed the package correctly.

Solution 8 - Java

I met the same problem and fail even after installing the 'selenium-server-standalone-version.jar', I think you need to install the guava and guava-gwt jar (https://code.google.com/p/guava-libraries/) as well. I added all of these jar, and finally it worked in my PC. Hope it works for others meeting this issue.

Solution 9 - Java

Please include all the jar files of selenium stand-alone and lib folder, then this error will resolved

Solution 10 - Java

I had the same issue. I found that I forgot to add selenium-2.53.0/selenium-java-2.53.0-srcs.jar file to my project's Reference library.

Solution 11 - Java

When I caught the exception java.lang.NoClassDefFoundError: com/google/common/base/Function it was caused by errors in Project Libraries.

Please check it in your project settings. For Intellij IDEA go to File - Project Structure and select Modules tab. All I needed to do to resolve this exception was re-adding the selenium library

Solution 12 - Java

After you extract your "selenium-java-.zip" file you need to configure your build path from your IDE. Import all the jar files under "lib" folder and both selenium standalone server & Selenium java version jar files.

Solution 13 - Java

I wanted to try a simple class outside IDE and stuff. So downloaded selenium zip from website and run the class like this:

java -cp selenium-2.50.1/*:selenium-2.50.1/libs/*:. my/package/MyClass <params>

I had the issue that I initially used lib instead of libs. I didn't need to add selenium standalone jar. This is Java 8 that understands wildcards in classpath. I think java 7 would also do.

Solution 14 - Java

I had the same problem, and finally I found that I forgot to add the selenium-server-standalone-version.jar. I had only added the client jar, selenium-java-version.jar.

Solution 15 - Java

this is for chrome  
System.setProperty("webdriver.chrome.driver","D:\\Testing_offical\\chromedriver.exe");
driver =new ChromeDriver();
this is for fire fox 
System.setProperty("webdriver.gecko.driver",""D:\\Testing_offical\\geckodriver.exe"");
driver =new FirefoxDriver();

pattern :

System.setProperty("webdriver.gecko.driver","**Path of the gecko driver** ");

Note download gecko from here :- http://docs.seleniumhq.org/download/

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionvijaymscView Question on Stackoverflow
Solution 1 - JavaHenry MaView Answer on Stackoverflow
Solution 2 - JavaBart KiersView Answer on Stackoverflow
Solution 3 - JavaThoView Answer on Stackoverflow
Solution 4 - JavapstantonView Answer on Stackoverflow
Solution 5 - JavaSumodView Answer on Stackoverflow
Solution 6 - JavaRahul kView Answer on Stackoverflow
Solution 7 - JavadonnytonView Answer on Stackoverflow
Solution 8 - JavaShi LiangjunView Answer on Stackoverflow
Solution 9 - JavaSandeep DuveView Answer on Stackoverflow
Solution 10 - JavaLearnerView Answer on Stackoverflow
Solution 11 - JavaIrina BuView Answer on Stackoverflow
Solution 12 - JavaCTesterView Answer on Stackoverflow
Solution 13 - JavaakostadinovView Answer on Stackoverflow
Solution 14 - JavaLokesh ReddyView Answer on Stackoverflow
Solution 15 - Javainderpreet kaurView Answer on Stackoverflow