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

时间:2022-08-30 00:06:09

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

错误:[Ljava.lang.StackTraceElement;@80f4cb

Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/base/Function

线程“main”java.lang中的异常。NoClassDefFoundError:com/google/common/base/Function


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

有人能帮我找到解决方法或理由吗?

15 个解决方案

#1


101  

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.

我遇到了同样的问题,最后我发现我忘了添加selenium-server-standalone-version.jar。我只添加了客户端jar, selenium-java-version.jar。

Hope this helps.

希望这个有帮助。

#2


22  

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.

当JRE找不到类时抛出NoClassDefFoundError。在您的示例中,它无法找到类com.google.common.base.Function,您很可能没有添加到类路径中。

EDIT

After downloading the following libraries:

下载下列图书馆:

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

解压缩它们并将所有JAR文件放在一个名为lib的文件夹中,测试类:

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

#3


7  

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

您的类路径上没有“google-collections”库。

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

java类路径路径/ lib。罐子…

#4


6  

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:

我遇到了同样的错误,在经过调查之后,我发现库selenium-api 2.41.0需要guava 15.0,但是它被旧版本覆盖了,所以我声明guava 15.0作为直接依赖项,通过在pom.xml中添加以下配置。

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

#5


2  

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.

对我来说,除了选择jar - selenium-java-2.45.0。jar,我必须在selenium根文件夹下的“libs”文件夹中选择所有jar。

#6


1  

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.

它并没有找到它的类函数。检查确保所有必需的库都在构建路径中,并且正确地输入了包。

#7


1  

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.

我遇到了同样的问题,甚至在安装了“selenium-server-standalone-version”之后也失败了。我认为您需要安装guava和guava-gwt jar (https://code.google.com/p/guava-libraries/)。我把所有的罐子都加进去了,最后它在我的电脑里工作。希望它能为其他遇到这个问题的人工作。

#8


1  

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

请包含selenium独立和lib文件夹的所有jar文件,然后这个错误将被解决。

#9


1  

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.

我有同样的问题。我发现我忘记添加selenium-2.53.0/selenium-java-2.53.0-srcs。jar文件到我的项目的参考图书馆。

#10


1  

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

我得到了相同的错误,但是如果您使用INTELIJ,如果您添加了selenium的库(如果没有),就会解决这个问题。

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

>projectStructure>模块>+>添加了selenium jar(来自lib文件夹和外部文件)。

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

对于其他IDE也需要同样的操作,比如eclipse。

#11


0  

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

当我捕捉到异常的java.lang。NoClassDefFoundError: com/google/common/base/Function它是由项目库中的错误引起的。

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

请在您的项目设置中查看。对于Intellij IDEA,转到文件-项目结构和选择模块标签。我需要做的就是重新添加selenium库。

#12


0  

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.

在您提取“selenium-java——”之后。压缩文件,您需要配置您的构建路径从您的IDE。导入“lib”文件夹下的所有jar文件,以及selenium独立服务器和selenium java版本jar文件。

#13


0  

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

我想在IDE之外尝试一个简单的类。因此,从网站下载selenium zip并运行类:

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.

我有一个问题,我最初使用lib而不是libs。我不需要添加selenium独立jar。这是在类路径中理解通配符的Java 8。我想java 7也可以。

#14


0  

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.

我遇到了同样的问题,最后我发现我忘了添加selenium-server-standalone-version.jar。我只添加了客户端jar, selenium-java-version.jar。

#15


0  

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/

请从这里下载gecko:- http://docs.seleniumhq.org/download/。

#1


101  

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.

我遇到了同样的问题,最后我发现我忘了添加selenium-server-standalone-version.jar。我只添加了客户端jar, selenium-java-version.jar。

Hope this helps.

希望这个有帮助。

#2


22  

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.

当JRE找不到类时抛出NoClassDefFoundError。在您的示例中,它无法找到类com.google.common.base.Function,您很可能没有添加到类路径中。

EDIT

After downloading the following libraries:

下载下列图书馆:

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

解压缩它们并将所有JAR文件放在一个名为lib的文件夹中,测试类:

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

#3


7  

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

您的类路径上没有“google-collections”库。

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

java类路径路径/ lib。罐子…

#4


6  

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:

我遇到了同样的错误,在经过调查之后,我发现库selenium-api 2.41.0需要guava 15.0,但是它被旧版本覆盖了,所以我声明guava 15.0作为直接依赖项,通过在pom.xml中添加以下配置。

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

#5


2  

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.

对我来说,除了选择jar - selenium-java-2.45.0。jar,我必须在selenium根文件夹下的“libs”文件夹中选择所有jar。

#6


1  

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.

它并没有找到它的类函数。检查确保所有必需的库都在构建路径中,并且正确地输入了包。

#7


1  

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.

我遇到了同样的问题,甚至在安装了“selenium-server-standalone-version”之后也失败了。我认为您需要安装guava和guava-gwt jar (https://code.google.com/p/guava-libraries/)。我把所有的罐子都加进去了,最后它在我的电脑里工作。希望它能为其他遇到这个问题的人工作。

#8


1  

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

请包含selenium独立和lib文件夹的所有jar文件,然后这个错误将被解决。

#9


1  

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.

我有同样的问题。我发现我忘记添加selenium-2.53.0/selenium-java-2.53.0-srcs。jar文件到我的项目的参考图书馆。

#10


1  

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

我得到了相同的错误,但是如果您使用INTELIJ,如果您添加了selenium的库(如果没有),就会解决这个问题。

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

>projectStructure>模块>+>添加了selenium jar(来自lib文件夹和外部文件)。

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

对于其他IDE也需要同样的操作,比如eclipse。

#11


0  

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

当我捕捉到异常的java.lang。NoClassDefFoundError: com/google/common/base/Function它是由项目库中的错误引起的。

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

请在您的项目设置中查看。对于Intellij IDEA,转到文件-项目结构和选择模块标签。我需要做的就是重新添加selenium库。

#12


0  

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.

在您提取“selenium-java——”之后。压缩文件,您需要配置您的构建路径从您的IDE。导入“lib”文件夹下的所有jar文件,以及selenium独立服务器和selenium java版本jar文件。

#13


0  

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

我想在IDE之外尝试一个简单的类。因此,从网站下载selenium zip并运行类:

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.

我有一个问题,我最初使用lib而不是libs。我不需要添加selenium独立jar。这是在类路径中理解通配符的Java 8。我想java 7也可以。

#14


0  

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.

我遇到了同样的问题,最后我发现我忘了添加selenium-server-standalone-version.jar。我只添加了客户端jar, selenium-java-version.jar。

#15


0  

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/

请从这里下载gecko:- http://docs.seleniumhq.org/download/。