Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.8k views
in Technique[技术] by (71.8m points)

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

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.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...