[java] NoClassDefFoundError in Java: com/google/common/base/Function

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?

This question is related to java selenium-webdriver runtime-error

The answer is


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.


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 ...


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>

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.


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


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.


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.


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.


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.


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

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.


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.


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.


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/


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


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to selenium-webdriver

SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 81 Selenium: WebDriverException:Chrome failed to start: crashed as google-chrome is no longer running so ChromeDriver is assuming that Chrome has crashed WebDriverException: unknown error: DevToolsActivePort file doesn't exist while trying to initiate Chrome Browser How to configure ChromeDriver to initiate Chrome browser in Headless mode through Selenium? How to make Firefox headless programmatically in Selenium with Python? Selenium Web Driver & Java. Element is not clickable at point (x, y). Other element would receive the click How do you fix the "element not interactable" exception? Scrolling to element using webdriver? Only local connections are allowed Chrome and Selenium webdriver Check if element is clickable in Selenium Java

Examples related to runtime-error

ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'undefined' what does Error "Thread 1:EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)" mean? javac: invalid target release: 1.8 C++ error : terminate called after throwing an instance of 'std::bad_alloc' Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException Error No Main class found in NetBeans Runtime error: Could not load file or assembly 'System.Web.WebPages.Razor, Version=3.0.0.0 TypeError: Cannot read property "0" from undefined Array Index Out of Bounds Exception (Java) Excel VBA Automation Error: The object invoked has disconnected from its clients