Exception in thread “main” java.lang.NoClassDefFoundError: okhttp3/ConnectionPool with Selenium and Java

被刻印的时光 ゝ 提交于 2020-01-24 23:40:11

问题


i have a simple Selenium Test Code:

public static void main(String[] args) {

    System.setProperty("webdriver.chrome.driver", "/home/chromedriver");
    WebDriver driver= new ChromeDriver();
    driver.get("http://google.com");
}

And i get this Error:

Exception in thread "main" java.lang.NoClassDefFoundError: okhttp3/ConnectionPool | Caused by: java.lang.ClassNotFoundException: okhttp3.ConnectionPool

I think the jars and Dependency are ok but i still get this Error


回答1:


please check of this jars files in your project libraries:

okhttp-3.10.0.jar & okio1.14.1.jar

may be solve using this jar files your problem :

java.lang.NoClassDefFoundError: okhttp3/ConnectionPool




回答2:


The error says it all :

Exception in thread "main" java.lang.NoClassDefFoundError: okhttp3/ConnectionPool | Caused by: java.lang.ClassNotFoundException: okhttp3.ConnectionPool

What is NoClassDefFoundError

NoClassDefFoundError in Java occurs when Java Virtual Machine is not able to find a particular class at runtime which was available at compile time. For example, if we have resolved a method call from a class or accessing any static member of a Class and that Class is not available during run-time then JVM will throw NoClassDefFoundError.

The error clearly says that you have misconfigured the classpath. It would be tough to debug the exact cause of the issue untill and unless you tell us how you run tests, which builder or IDE do you use and the builder config file or project description.

What went wrong :

From all the above mentioned points it's clear that the related Class or Methods were resolved from one source Compile Time which was not available during Run Time.

This situation occurs if there are presence of multiple sources to resolve the Classes and Methods through JDK/Maven/Gradle.

Selenium dependency on okhttp

At this point it is worth to mention that selenium-java-3.9.x clients does have a dependency on okhttp and you can find the dependency list here.

It is also to be noted that :

  • There were some issues with launching of Chrome as per Can't launch chrome browser using latest selenium 3.9.0.

  • To address the issue from Selenium v3.9.1 OkHttp backed instances can now connect to servers requiring authorisation which was based on PR #5444.

Solution :

Here are a few steps to solve NoClassDefFoundError - okhttp3/ConnectionPool error :

  • While using a Build Tool e.g. Maven or Gradle, remove all the External JARs from the Java Build Path. Maven or Gradle will download and resolve all the required dependencies.
  • If using Selenium JARs within a Java Project add only required External JARs within the Java Build Path and remove the unused one.
  • While using Maven, either use <artifactId>selenium-java</artifactId> or <artifactId>selenium-server</artifactId>. Avoid using both at the same time.
  • Upgrade JDK to recent levels JDK 8u162.
  • Upgrade Selenium to current levels Version 3.10.0.
  • Upgrade ChromeDriver ChromeDriver v2.37 level.
  • Keep Chrome version at Chrome v64-66 levels. (as per ChromeDriver v2.37 release notes)
  • Clean your Project Workspace through your IDE and Rebuild your project with required dependencies only.
  • Use CCleaner tool to wipe off all the OS chores before and after the execution of your test Suite.
  • If your base Chrome version is too old, then uninstall it through Revo Uninstaller and install a recent GA and released version of Chrome.
  • Execute your @Test.



回答3:


I hope, your trying a method without having class. Please try to put your main method inside class. please let me know if you are getting any error further.

class Test{

public static void main(String[] args) {

System.setProperty("webdriver.chrome.driver", "youHaveToUseLocationWhereYouHaveYourChromeDriver");
WebDriver driver= new ChromeDriver();

driver.get("http://google.com");

} }



来源:https://stackoverflow.com/questions/49404956/exception-in-thread-main-java-lang-noclassdeffounderror-okhttp3-connectionpoo

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