I am programming in Java using Maven/Eclipse.
I am able to run HtmlUnit fine when running from a Unit Test. (test/)
However when I try to use the same code in the src/ folder, I receive java.lang.NoClassDefFoundError messages. The only way I have been able to resolve them is to go and manually add all the jars to the build path. But this doesn’t make sense to me as the jar file shows up in my Maven Dependencies.
pom.xml (there are more dependencies in the actual pom file)
<dependency>
<groupId>net.sourceforge.htmlunit</groupId>
<artifactId>htmlunit</artifactId>
<version>2.9</version>
<scope>test</scope>
</dependency>
The Sample block of HtmlUnit code
WebClient webClient = new WebClient();
HtmlPage page = webClient.getPage(url);
System.out.println("Executing Pages JavaScript for " + config.executeJavaScriptTime + " seconds..."); webClient.waitForBackgroundJavaScript(config.executeJavaScriptTime);
dom = cleaner.clean(page.asXml());
html = cleaner.getInnerHtml(dom);
webClient.closeAllWindows();
Any Ideas? Thanks.
The test scope which is applied to your dependency means that it is not available on the compile classpath. This way, the code you ship does not depend on test code. A more complete explanation can be found here. If the project you are building is intended to be only tests, you should remove the scope tag to let it take the default scope, compile. But in general it is correct that your shipping code, built from src, shouldn’t depend on JUnit, a testing library.