java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkState(ZLjava/lang/String;) 与 Selenium、gradle 和 ChromeDriv
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49021707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkState(ZLjava/lang/String;) with Selenium, gradle and ChromeDriver
提问by Swati Thorve
I am trying to use Selenium api with Gradle. This is my build.gradle dependency section:
我正在尝试将 Selenium api 与 Gradle 一起使用。这是我的 build.gradle 依赖项部分:
dependencies {
compile 'com.google.api-client:google-api-client:1.23.0'
compile 'com.google.oauth-client:google-oauth-client-jetty:1.23.0'
compile 'com.google.apis:google-api-services-sheets:v4-rev506-1.23.0'
compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '2.9.0'
compile group: 'org.seleniumhq.selenium', name: 'selenium-chrome-driver', version: '2.9.0' }
My selenium - Java code:
我的硒 - Java代码:
System.setProperty("webdriver.chrome.driver", "C:\Program Files(x86)\Google\Chrome\Application\chrome.exe");
WebDriver driver = new ChromeDriver();
Code works fine, and I am able to get Chrome browser opened. However, in build.gradle, I am using 2.9.0 version of selenium and chromedriver. If I try to use any version after 2.9.0, it gives me below error in WebDriver driver = new ChromeDriver(); method:
代码工作正常,我可以打开 Chrome 浏览器。但是,在 build.gradle 中,我使用的是 2.9.0 版本的 selenium 和 chromedriver。如果我尝试使用 2.9.0 之后的任何版本,它会在 WebDriver driver = new ChromeDriver(); 中给我以下错误。方法:
Exception in thread "main" java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkState(ZLjava/lang/String;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)V
at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:124)
at org.openqa.selenium.chrome.ChromeDriverService.access<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>27.1-jre</version>
</dependency>
0(ChromeDriverService.java:32)
at org.openqa.selenium.chrome.ChromeDriverService$Builder.findDefaultExecutable(ChromeDriverService.java:137)
at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:339)
at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:88)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:123)
at Quickstart.main(Quickstart.java:130)
I tried looking for gradle+Maven+selenium supported version. Was not able to find any good info. Any idea?
我尝试寻找 gradle+Maven+selenium 支持的版本。无法找到任何好的信息。任何的想法?
回答by Reza
Try to update your Guava to
尝试将您的番石榴更新为
Exception in thread "main" java.lang.NoSuchMethodError:
com.google.common.base.Preconditions.checkState(ZLjava/lang/String;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)V
It will solve your issue.
它将解决您的问题。
回答by DebanjanB
This error message...
这个错误信息...
System.setProperty("webdriver.chrome.driver", "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe");
WebDriver driver = new ChromeDriver();
...implies that the Java Clientwas unable to find ChromeDriver()
.
...表示Java 客户端无法找到ChromeDriver()
.
Issue & Solution
问题与解决方案
As per the Selenium - Java codeyou have shared, the System.setProperty()
line is used to set the ChromeDriverbinary path not the chrome binary path. For that you have to download the ChromeDriverbinary from the ChromeDriver - WebDriver for Chromeand place it in your system and mention the absolute path of the ChromeDriverthrough System.setProperty()
line. Hence you have to change :
根据您共享的Selenium-Java 代码,该System.setProperty()
行用于设置ChromeDriverbinary path而不是 chrome binary path。为此,您必须从ChromeDriver - WebDriver for Chrome下载ChromeDriver二进制文件并将其放置在您的系统中,并通过line提及ChromeDriver的绝对路径。因此你必须改变:System.setProperty()
System.setProperty("webdriver.chrome.driver", "C:\path\to\chromedriver.exe");
WebDriver driver = new ChromeDriver();
To :
到 :
/node_modules/selenium-standalone/bin/selenium-standalone start
回答by Paul Isaris
I have the exact same problem (I am using Maven though).
我有完全相同的问题(虽然我正在使用 Maven)。
I noticed that the problem is that using one of com.google.api-client
, or com.google.oauth-client
, or com.google.apis:google-api-services-sheets
alongside org.seleniumhq.selenium
causes the error.
我注意到的问题是,使用一个com.google.api-client
,或者com.google.oauth-client
,或者com.google.apis:google-api-services-sheets
沿着org.seleniumhq.selenium
导致错误。
The problem is that both dependencies depend on a different com.google.guava:guava
artifact.
问题是这两种依赖都依赖于不同的com.google.guava:guava
工件。
In order to solve the error, you should explicitly depend on a single com.google.guava:guava
artifact.
So go ahead and add the following in your build.gradle
:
为了解决错误,您应该明确依赖单个com.google.guava:guava
工件。因此,继续并在您的 中添加以下内容build.gradle
:
compile 'com.google.guava:guava:27.0.1-jre'
compile 'com.google.guava:guava:27.0.1-jre'
回答by kamranicus
Just wanted to post here in case anyone else comes to this from Google like I did. For whatever reason, I needed to run with sudo
. I was having issues using the npm selenium-standalone
package and running:
只是想在这里发帖,以防其他人像我一样从谷歌来到这里。无论出于何种原因,我都需要使用sudo
. 我在使用 npmselenium-standalone
包并运行时遇到问题:
sudo /node_modules/selenium-standalone/bin/selenium-standalone start
And it would show that error. What fixed it was running with sudo
它会显示那个错误。什么修复了它与 sudo 一起运行
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.1.0</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/io.rest-assured/rest-assured -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>3.0.0</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/io.rest-assured/json-path -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>json-path</artifactId>
<version>3.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.rest-assured/json-schema-validator -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>json-schema-validator</artifactId>
<version>3.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.rest-assured/xml-path -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>xml-path</artifactId>
<version>3.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hamcrest/java-hamcrest -->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>java-hamcrest</artifactId>
<version>2.0.0.0</version>
<scope>test</scope>
</dependency>
<!-- cucumber dependency begins -->
<!-- https://mvnrepository.com/artifact/net.masterthought/cucumber-reporting -->
<dependency>
<groupId>net.masterthought</groupId>
<artifactId>cucumber-reporting</artifactId>
<version>4.7.0</version>
</dependency>
<!-- starts here -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-core</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.2.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-jvm</artifactId>
<version>1.2.5</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.2.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-jvm-deps</artifactId>
<version>1.0.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-html -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-html</artifactId>
<version>0.2.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/info.cukes/gherkin -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>gherkin</artifactId>
<version>2.12.2</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-testng -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-testng</artifactId>
<version>5.4.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.theoryinpractise/cucumber-testng-factory -->
<dependency>
<groupId>com.theoryinpractise</groupId>
<artifactId>cucumber-testng-factory</artifactId>
<version>1.0.1</version>
</dependency>
<!-- https://stackoverflow.com/questions/49021707/java-lang-nosuchmethoderror-com-google-common-base-preconditions-checkstatezlj?rq=1 -->
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>27.1-jre</version>
</dependency>
</dependencies>
I don't think I needed to do this before but suddenly it's the only way it works now.
我认为我以前不需要这样做,但突然间这是它现在唯一可行的方法。
回答by John M Iriye Esq
I had the same problem and ran a dependency check and found that there were conflicts. The solution that worked for me was to exclude the conflicting dependencies.
我遇到了同样的问题并运行了依赖项检查,发现存在冲突。对我有用的解决方案是排除冲突的依赖项。
Your project will probably have different dependencies than mine. So, listing the specific conflicts in my project may not be helpful.
您的项目可能与我的项目具有不同的依赖项。因此,列出我的项目中的具体冲突可能没有帮助。
回答by Nadia Sawda
copy and paste the following dependencies in the pom.xml and then do a maven build:
将以下依赖项复制并粘贴到 pom.xml 中,然后进行 maven 构建:
##代码##