NoClassDefFoundError in Java: com/google/common/ba

2019-01-02 23:47发布

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?

15条回答
做个烂人
2楼-- · 2019-01-03 00:07

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.

查看更多
Animai°情兽
3楼-- · 2019-01-03 00:12

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

查看更多
叛逆
4楼-- · 2019-01-03 00:14

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
查看更多
Root(大扎)
5楼-- · 2019-01-03 00:14

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.

查看更多
Viruses.
6楼-- · 2019-01-03 00:21

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
7楼-- · 2019-01-03 00:23

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

查看更多
登录 后发表回答