Selenium-TestNG-Maven – Getting “java.lang.NoClassDefFoundError: org/openqa/selenium/firefox/FirefoxDriver”

What is NoClassDefFoundError

NoClassDefFoundError in Java occurs when JVM 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 runtime then JVM will throw NoClassDefFoundError.

The error you are seeing is :

java.lang.NoClassDefFoundError: org/openqa/selenium/firefox/FirefoxDriver

This clearly indicates that Selenium is trying to resolve the particular FirefoxDriver Class at runtime from org/openqa/selenium/firefox/FirefoxDriver which is not available.

What went wrong :

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

From the pom.xml it is pretty clear that you have added multiple dependencies for FirefoxDriver Class as follows:

  • <artifactId>selenium-java</artifactId>:

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.12.0</version>
        <scope>test</scope>
    </dependency>
    
  • <artifactId>selenium-firefox-driver</artifactId>:

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-firefox-driver</artifactId>
        <version>3.12.0</version>
    </dependency>
    
  • <artifactId>selenium-server</artifactId>:

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-server</artifactId>
        <version>3.12.0</version>
    </dependency>
    
  • Additionally you have also added all jar files.

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

Solution :

Here are a few steps to solve NoClassDefFoundError :

  • 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 all the dependencies mentioned in the configuration file (e.g. pom.xml) to resolve the Classes and Methods.
  • If using Selenium JARs within a Java Project add only required External JARs within the Java Build Path and remove the unused and duplicate External JARs.
  • If you are using FirefoxDriver use either of the <artifactId>selenium-java</artifactId> or <artifactId>selenium-server</artifactId>. Avoid using both at the same time.
  • Remove the unwanted and duplicated from pom.xml
  • 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 Web Client version is too old, then uninstall it through Revo Uninstaller and install a recent GA and released version of Web Client.
  • Take a System Reboot.
  • While you execute a Maven Project always perform the folling in sequence:

    • maven clean
    • maven install
    • maven test

You can find related discussions in:

Leave a Comment