Exception in thread "main" java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver
Asked Answered
R

9

14

I have added the most updated Selenium dependency in my pom.xml

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.7.1</version>
</dependency>

I ran mvn clean install inside the directory with my pom.xml and I have also imported the correct classes in my app class as per the Selenium documentation

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

However when i try and run my main method, I get the following error

Exception in thread "main" java.lang.NoClassDefFoundError: 
org/openqa/selenium/WebDriver

I look in my ~/.m2/repository folder and I don't see an openqa folder but instead I see a seleniumhq folder.

Why didn't maven install the openqa folder, and why does the documentation say to import from org.openqa... when that never exist in my jar repository. I'm very confused, I just want to be able to import selenium Webdriver successfully while having it in my local repository.

Ritchey answered 14/12, 2017 at 23:9 Comment(1)
You are probably doing the very same kind of mistake as the OP at https://mcmap.net/q/830488/-connect-java-project-to-mongodb-database/1525602, i. e. not having all the jar dependencies (including the transitive ones!) in the classpath when you run your program. So here's the crucial question: How do you run your program?. Because there are way too many ways of how to build your application bundle depending on how you want to run it (from an IDE, from command line, from command line using mvn exec, as a servlet on a server...).Tracheid
P
6

Encountered this error in Eclipse IDE. In Eclipse go to Project properties and in Java Build Path just add selenium jars in Classpath instead of Modulepath. Then under the Project tab on the top do a Clean to remove earlier buiid and then do a Run.

Peri answered 20/12, 2020 at 15:0 Comment(0)
C
5

Firstly, check properly if you have all important dependencies for your program.
Secondly, I had similar error while running maven project:

Caused by: java.lang.NoClassDefFoundError: org/openqa/selenium/JavascriptExecutor

And this problem was because of inappropriate plugin, because I tested different versions of Selenium and it didn't help me.

So when I changed maven-jar-plugin:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
 <configuration>
   <archive>
        <manifest>
             <addClasspath>true</addClasspath>
             <classpathPrefix>lib/</classpathPrefix>
             <mainClass>your_main_class</mainClass>
        </manifest>
   </archive>
 </configuration>
</plugin>

to maven-shade-plugin plugin:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.0.0</version>
    <executions>
       <execution>
            <phase>package</phase>
            <goals>
               <goal>shade</goal>
            </goals>
            <configuration>
                 <transformers>
                    <transformer 
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>your_main_class</mainClass>
                    </transformer>
                 </transformers>
             </configuration>
       </execution>
    </executions>
</plugin>

The issue was gone. The difference between plugins you can find here.


In addition, sometimes we upgrade our libraries even with same method name. Due this different in version, we get NoClassDefFoundError or NoSuchMethodError at runtime when one library was not compatible with such an upgrade.

Java build tools and IDEs can also produce dependency reports that tell you which libraries depend on that JAR. Mostly, identifying and upgrading the library that depends on the older JAR resolve the issue.


To summarize:

  • try to change versions of Selenium, if it contains all dependencies;
  • try to add necessary dependencies if you don't have it;
  • try to check folder of maven if it has or not what says specific error;
  • try to play with plugins if nothing helps above.
Comptroller answered 13/8, 2019 at 11:45 Comment(1)
Brilliant! This has fixed my issue right away. I understand what the problem was but what I didn't understand was why wouldn't mvn package all the dependencies needed at run-time in the jar. Maybe a better configuration of the jar plugin specifying what was needed would have worked anyway... I am going to try that as wellShalon
W
4

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 you are seeing is :

Exception in thread "main" java.lang.NoClassDefFoundError: 
org/openqa/selenium/WebDriver

This clearly indicates that Selenium is trying to resolve the particular class at runtime from org/openqa/selenium/WebDriver which is no more available.

As you mentioned of looking into ~/.m2/repository folder, the maven folder structure for Selenium v3.7.1 (on Windows) is as follows :

C:\Users\<user_name>\.m2\repository\org\seleniumhq\selenium\selenium-java\3.7.1

So when you see a seleniumhq folder, it is pretty much expected.


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.


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 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.
  • Remove the unwanted other <dependency> from pom.xml
  • Clean you Project Workspace within your IDE periodically only to build your project with required dependencies.
  • Use CCleaner tool to wipe away the OS chores periodically.
  • While you execute a Maven Project always do maven clean, maven install and then maven test.
Whooper answered 16/12, 2017 at 11:22 Comment(1)
I think the 1st part, "What is NoClassDefFoundError", of this answer is quite correct. But I'm afraid the 2nd part, "What went wrong", contains many expectations and recommendations which probably aren't to the point. Like to use CCleaner or to always do maven ... to name some... This can confuse an inexperienced Maven user, I think.Tracheid
U
1

Are you using an IDE or working from command line? In Eclipse for example you can force downloading all dependencies by right clicking on your project, going to Maven menu item and then selecting Update Project. Then check the "Force Update of Snapshots/Releases" checkbox.

If you are opening from command line do:

mvn clean install -U

from your project path.

Umbilicus answered 15/12, 2017 at 3:17 Comment(1)
The OP writes that the Maven build and compilation was OK. So there is probably no need to force Maven to re-download the artifacts (jar-s etc.) as you suggest.Tracheid
C
1

This is happening because you are selecting jar files under modulepath, you should add them under class path.

Cecilla answered 14/3, 2022 at 8:36 Comment(0)
A
0
  • org.openqa.selenium is the package in the selenium-api-{version}.jar under the seleniumhq\selenium\selenium-api folder.
  • org.openqa.selenium.firefox is the package in the selenium-firefox-driver-{version}.jar under the seleniumhq\selenium\selenium-firefox-driver folder.

So there is no openqa folder, it's just the package name under the seleniumhq folder, you should have a check into these jar.

It's hard to say what caused NoClassDefFoundError exception without project structure and code detail. The exception is not the same as ClassNotFoundException. Maybe this answer https://mcmap.net/q/15848/-why-am-i-getting-a-noclassdeffounderror-in-java would be helpful.

Angeles answered 15/12, 2017 at 3:11 Comment(0)
S
0

What worked for me was to add this dependency to pom.xml:

    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>25.0-jre</version>
    </dependency>
Selfeffacement answered 14/7, 2019 at 9:54 Comment(1)
I guess this is not necessary. Because this dependency is already there, although in version 23. It is one of the transitive dependencies you can see at the mvnrepository page, for example. Or do you really say that using another Guava version did the trick?Tracheid
C
0

I was getting below error from past 2 days and what helped me was to remove all the selenium extra dependencies like selenium-support, selenium-chrome-driver etc and only keeping the below dependencies in POM file.

Error:-

java.lang.NoClassDefFoundError: org/openqa/selenium/HasAuthentication

at java.base/java.lang.ClassLoader.defineClass1(Native Method)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1012)
at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:150)
at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:862)
at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:760)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:681)

Dependencies in the pom file after removing all other:-

<dependencies>
    <dependency>
                <groupId>org.seleniumhq.selenium</groupId>
                <artifactId>selenium-java</artifactId>
                <version>4.1.1</version>
            </dependency>
            <dependency>
                <groupId>org.testng</groupId>
                <artifactId>testng</artifactId>
                <version>7.4.0</version>
                <scope>test</scope>
            </dependency>
    </dependencies>
Crapulous answered 10/3, 2022 at 21:41 Comment(0)
S
0

Have encountered this issue while running selenium test in eclipse IDE.

Navigate to following path:

1.Properties >> Java build path >> Libraries.

2.Add all selenium jars in Classpath instead of Modulepath.

3.Apply and close modal.

4.Now go to build path and click on "Configure Build Path".

5.Now run the selenium test.

Swinge answered 15/6, 2022 at 17:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.