java.lang.NoClassDefFoundError: org/w3c/tidy/Tidy , Maven project


I am trying to run java application built in Maven 2 from command prompt

On command prompt I did "mvn package" and got the jar in target folder of the application

Then I did:-

java -cp target/TempestApp.jar foo.App

And I get exception:-

Exception in thread "main" java.lang.NoclassDefFoundError: org/w3c/tidy/Tidy
      at foo.htmltoxml.HtmlToXMLConvertor(htmltoxml.java:29)
      at foo.app.main(App.java:35)

The application comprises of a JTidy. Who's dependency is mention in the POM.xml

Application runs fine in Eclipse but not from command prompt.

I even tried putting the JTidy jar in classpath variable


Answers:


You can let maven start your application with all the jars in the classpath. It is quite easy with the exec-maven-plugin.

Add this into your pom.xml:

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2.1</version>
            <executions>
                <execution>
                    <goals>
                        <goal>java</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <mainClass>com.company.package.YourApplicationMain</mainClass>
            </configuration>
        </plugin>
    </plugins>
</build>

Then when you want to build and execute in the same command:

mvn package exec:java

And if you just want to run the application:

mvn exec:java