3

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

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Bota723
  • 31
  • 1
  • 1
  • 4
  • Possible duplicate of [How to solve java.lang.NoClassDefFoundError?](https://stackoverflow.com/questions/17973970/how-to-solve-java-lang-noclassdeffounderror) – Dheemanth Bhandarkar Mar 21 '18 at 11:33

4 Answers4

4

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.
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thank you for your comment. Im working on Eclipse on a Linux OS. I am now up to date on the jdk, selenium, chromedriver and chrome, but still have the same problem. Like you said the problem should be with the dependency, but i fail to understand how Maven works. I just added the .jars to the project and then added them to the buildpath. Is there a way to set the selenium-java-3.9.x -> okhttp dependency without maven? – Bota723 Mar 21 '18 at 14:34
  • Just the client-combined-3.11.0.jar and for some testing the okhttp-3.9.1.jar left, still the same problem. – Bota723 Mar 21 '18 at 18:14
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

s__
  • 9,270
  • 3
  • 27
  • 45
surendra
  • 29
  • 2
0

I had the same issue as this. By following the advice above AND adding the Selenium client JARs(including sources) to modulepath and the Selenium server JAR to classpath it worked.

-1

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");

} }

Guna Yuvan
  • 101
  • 11