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

15,798

Solution 1

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 :

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.

Solution 2

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

Share:
15,798
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    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