1

I'm creating maven application where I'm getting information from website.

I created maven package, but when I run jar using command: "java -jar app-1.0-SNAPSHOT.jar" I get this error:

"java.lang.ClassNotFoundException: org.openqa.selenium.WebDriver"

It works good when I'm running it in IntelliJ.

This is my pom:

<groupId>app</groupId>
<artifactId>app</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>2.53.1</version>
</dependency>
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-htmlunit-driver</artifactId>
    <version>2.52.0</version>
</dependency>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>itextpdf</artifactId>
        <version>5.0.6</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>sample.Main</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.10</version>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <includeScope>runtime</includeScope>
                        <outputDirectory>${project.build.directory}/dependency-jars/</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Please help me find out what is wrong. Thanks!

Liam
  • 53
  • 9

1 Answers1

0

there may be a chance of version mismatch as follows:

Starting with 2.53.0 you need to explicitly include HtmlUnitDriver as a dependency to include it. Version number of the driver is now tracking HtmlUnit itself.

 <dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>htmlunit-driver</artifactId>
    <version>2.53.1</version>
</dependency>  

Keep same versions of Selenium-Java and HTMLUnitDriver.

OR

There might be a dependency on selenium-server-standalone.jar

If you are using DefaultSelenium (or the RemoteWebDriver implementation), you still need to start a Selenium server. The best way is to download the selenium-server-standalone.jar from the Selenium Downloads page and just use it. Furthermore you can also embed the Selenium server into your own project, if you add the following dependency to your pom.xml:

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-server</artifactId>
    <version>3.0.1</version>
</dependency>  

Note: Be aware, that the selenium-server artifact has a dependency to the servlet-api-2.5 artifact, which you should exclude, if your project will be run inside a web application container.

Reference:

http://www.seleniumhq.org/download/maven.jsp

Naveen Kumar R B
  • 6,248
  • 5
  • 32
  • 65
  • Thanks for your response. Unfortunatelly it didn't help. Htmlunit driver newest version is 2.52.0. I changed version of selenium-java to 2.52.0 but without success. – Liam Nov 14 '16 at 07:21
  • did you try adding dependency of selenium-server (for version 2.52.0) also ? – Naveen Kumar R B Nov 14 '16 at 07:38
  • Yes, still the same situation :( – Liam Nov 14 '16 at 07:56
  • Did you see what all dependencies (jars) are added under **Maven Dependencies" in your project? and is it working when you run the maven project directly (from an IDE) instead of using its jar? I tried with above pom.xml using Eclipse by creating Maven project and it is working for me. – Naveen Kumar R B Nov 14 '16 at 08:28
  • And also try by adding selenium-api 2.53.0 jar as dependency. Keep all dependencies version same. – Naveen Kumar R B Nov 14 '16 at 08:30
  • Adding selenium-api didn't help. In "Dependencies" I have all dependencies from pom.xml: -selenium-java -selenium htmlunit-driver -selenium-api -itext -selenium-server And yes, when I run it from IntelliJ it works fine. – Liam Nov 14 '16 at 09:32
  • try this http://stackoverflow.com/a/4323501/2575259. I am able to understand that it is a problem of exporting the dependencies with the maven project. so, search for answer in that direction. – Naveen Kumar R B Nov 14 '16 at 09:48
  • 1
    http://stackoverflow.com/a/574650/4932401 this plugin helped: maven-assembly-plugin ;) Thanks for your time! – Liam Nov 14 '16 at 09:52