0

I wrote a small Maven Project in Netbeans using the Selenium HtmlUnitDriver. The Project works when i'm starting it from the IDE. But when i'm using the command java -jar to open the jar file it reports an Error.

Exception in thread "main" java.lang.NoClassDefFoundError: org/openqa/selenium/htmlunit/HtmlUnitDriver
    at com.mycompany.myPackage.classNameUI.<init>(classNameUI.java:26)
    at com.mycompany.myPackage.mainClass.main(mainClass.java:20)
Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.htmlunit.HtmlUnitDriver
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 2 more

the manifest.mf got the right Mainclass: path

Versions:

Netbeans 8.2

Maven 4.0.0

Selenium 2.53.1

pom.xml:

<?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>com.mycompany</groupId>
<artifactId>MyProject</artifactId>
<version>User</version>
<packaging>jar</packaging>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>2.53.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.2</version>
    </dependency>
    <dependency>
        <groupId>net.sourceforge.htmlunit</groupId>
        <artifactId>htmlunit</artifactId>
        <version>2.25</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>com.mycompany.mypackage.mainClass</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

n3knrg
  • 3
  • 2
  • Check (with Ctrl+O in NetBeans) in which JAR you find the HtmlUnitDriver class. The Exception says that this JAR is missing from the classpath. When you use "java -jar" you also have to define extra JARs on the classpath or include them in inside your JAR as explained in the post by Asettouf. – toongeorges Feb 24 '17 at 15:32

1 Answers1

0

I think it might be because you don't include in your classpath the dependencies your program is using, to solve this:

Add to the "maven-jar-plugin" under <addClasspath>:

<classpathPrefix>lib/</classpathPrefix>

Then add this plugin to the build section in your pom.xml:

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

See the doc for more details: https://maven.apache.org/plugins/maven-dependency-plugin/usage.html

Adonis
  • 4,670
  • 3
  • 37
  • 57