Selenium in Java stops working with java.lang.IllegalAccessError:from class org.openqa.selenium.net.UrlChecker when ZXing dependency is added

非 Y 不嫁゛ 提交于 2021-02-05 05:29:08

问题


So, I'm building a test project on java using Selenium, with gradle. Right now I need to scan a QR Code from a previously taken screenshot. I looked around how to do it, and the ZXing scanner code seems like the best suggestion. (Please let me know if it isn't.) My problem is, from the moment I add the 'com.google.zxing:zxingorg:3.3.1' dependency to my build.gradle file, even if I don't write any additional code with it (I've tried with and without), the web driver stops working, and I get this message:

java.lang.IllegalAccessError: tried to access method com.google.common.util.concurrent.SimpleTimeLimiter.<init>(Ljava/util/concurrent/ExecutorService;)V from class org.openqa.selenium.net.UrlChecker

at org.openqa.selenium.net.UrlChecker.<init>(UrlChecker.java:67)
at org.openqa.selenium.remote.service.DriverService.waitUntilAvailable(DriverService.java:175)
at org.openqa.selenium.remote.service.DriverService.start(DriverService.java:166)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:78)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:241)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:128)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:141)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:174)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:163)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:152)
at Specification.ClientFactory.initContext(ClientFactory.groovy:81)
at Specification.ClientFactory.<init>(ClientFactory.groovy:61)
at Specification.BaseTest.setupSpec(BaseTest.groovy:14)


Test ignored.

The code in which this error appears is the code I use to start the Selenium WebDriver:

ClientFactory(){
    initUrl()   //allows to change the URL of the application under test when needed
    initContext(urlWebsite,initWebBrowser())    //switchcase to read the browser from a config file
    initClient()    //initialization of the different classes with the elements I'm accessing
}

The line 61 referred in the error refers to the initialisation of the Chrome WebDriver:

webDriver = new ChromeDriver(options)

(I tried disabling the options, and the error I get is exactly the same, so I don't think the problem comes from there.)

I looked quite a while for it already, and I didn't find anything regarding this error. Is there any conflict between the Selenium and ZXing dependencies that I don't know about? If so, is there a way to surpass it? How?

UPDATE: for whoever happens to face a similar problem, this issue won't happen if you regress to earlier versions of selenium-java and ZXing. I managed to overcome the problem with 'org.seleniumhq.selenium:selenium-java:3.0.1' and 'com.google.zxing:zxingorg:3.2.1' in my build.gradle file, which aren't the latest, but it works this way


回答1:


The error says it all :

java.lang.IllegalAccessError: tried to access method com.google.common.util.concurrent.SimpleTimeLimiter.<init>(Ljava/util/concurrent/ExecutorService;)V from class org.openqa.selenium.net.UrlChecker

The main exception is from class org.openqa.selenium.net.UrlChecker. If you look at the JavaDocs of UrlChecker Class i.e. org.openqa.selenium.net.UrlChecker extends java.lang.Object which Polls a URL until a HTTP 200 response is received.

The nested class is UrlChecker.TimeoutException which extends java.lang.Exception

So once constructor TimeoutException(java.lang.String s, java.lang.Throwable throwable) failed java.lang.IllegalAccessError was raised which implies that an application attempted to call a method which it does not have access.

Usually this error is caught by the Java Compiler and this error can only occur at run time if the definition of a class has changed.

An immediate solution would be to check the call to the url and ensure that the get("your_url") is resolved through void org.openqa.selenium.WebDriver.get(String arg0).

Also ensure that the subnet or firewall settings are not blocking the http requests.




回答2:


For anybody reading this. I was facing the same issue with dependencies:

I had a hard dependency on com.google.guava:guava:23.3 or superior

+--- com.github.ben-manes.caffeine:guava:2.6.0
|    +--- com.github.ben-manes.caffeine:caffeine:2.6.0
|    \--- com.google.guava:guava:23.3-jre (*)

And was using org.seleniumhq.selenium:selenium-java:3.0.1 which is incompatible with guava versions > 22.0 as discussed here:

https://github.com/SeleniumHQ/selenium/issues/4381

SOLUTION: upgrading selenium to the latest solved that issue as discussed here



来源:https://stackoverflow.com/questions/48519580/selenium-in-java-stops-working-with-java-lang-illegalaccesserrorfrom-class-org

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!