0

I am trying to run the basic Selenium example. have specified the pom.xml as such, to include an entry point for an executable .jar:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>MySel20Proj</groupId>
    <artifactId>MySel20Proj</artifactId>
    <version>1.0</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>main.java.SeleniumTest</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>2.48.2</version>
        </dependency>
    </dependencies>
</project>

I can run a Maven install; however, when I execute the jar containing sample code from the Selenium tutorial, I receive a NoClassDefFoundError: /org/openqa/selenium/webdriver, suggesting that the Maven install of the selenium-java jar has failed.

Is there a way to resolve this without adding the selenium jars to the local classpath? It seems that solutions to this not building from Maven Central are considered harmful.

manglano
  • 844
  • 1
  • 7
  • 21
  • 1
    Was there a reason you specified the dependency type as "pom"? – FriedSaucePots Oct 14 '15 at 20:28
  • A StackOverflow solution to a similar issue suggested it as a possible fix; it does not help. Additionally, the required resources are in IntelliJ's "External Libraries" tree, and the dependent code is free of compiler errors. That is, the classes have been imported, but are not added to the .jar... – manglano Oct 14 '15 at 20:32
  • are u trying to execute the jar file through command prompt? – StrikerVillain Oct 14 '15 at 21:13
  • 1
    "suggesting that the Maven install of the selenium-java jar has failed" - odd conclusion. If a class cannot be found by Java then that means the class is not on the classpath; nothing more, nothing less. If the jar fails to download or install, Maven will tell you. – Gimby Oct 14 '15 at 21:15

1 Answers1

1

I think the issue is that you are not adding the dependent jars (Selenium in this case) to the jar containing to your sample code. You can add the following plugin to add Selenium to your sample code jar.

<build>
    <plugins>
      <!-- any other plugins -->
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
      </plugin>
    </plugins>
  </build>

How can I create an executable JAR with dependencies using Maven?

Including dependencies in a jar with Maven

Community
  • 1
  • 1
StrikerVillain
  • 3,719
  • 2
  • 24
  • 41