Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java HtmlUnit-在src文件夹中找不到类,但在测试文件夹中运行_Java_Maven_Htmlunit - Fatal编程技术网

Java HtmlUnit-在src文件夹中找不到类,但在测试文件夹中运行

Java HtmlUnit-在src文件夹中找不到类,但在测试文件夹中运行,java,maven,htmlunit,Java,Maven,Htmlunit,我正在使用Maven/Eclipse用Java编程 当从单元测试运行时,我能够很好地运行HtmlUnit。(测试/) 但是,当我尝试在src/文件夹中使用相同的代码时,我会收到java.lang.NoClassDefFoundError消息。我能够解决这些问题的唯一方法是手动将所有JAR添加到构建路径中。但这对我来说没有意义,因为jar文件显示在我的Maven依赖项中 xml(在实际的pom文件中有更多的依赖项) 有什么想法吗?谢谢。到目前为止,我发现的唯一可行的方法是创建一个注释为@Test的

我正在使用Maven/Eclipse用Java编程

当从单元测试运行时,我能够很好地运行HtmlUnit。(测试/)

但是,当我尝试在src/文件夹中使用相同的代码时,我会收到
java.lang.NoClassDefFoundError
消息。我能够解决这些问题的唯一方法是手动将所有JAR添加到构建路径中。但这对我来说没有意义,因为jar文件显示在我的Maven依赖项中

xml(在实际的pom文件中有更多的依赖项)


有什么想法吗?谢谢。

到目前为止,我发现的唯一可行的方法是创建一个注释为
@Test
的方法,并将爬虫程序作为JUnit测试运行

i、 e

我希望能够从标准的main方法运行它,即

public class Crawler() {

    public static void main(String[] args) {
        Crawler crawler = new Crawler();
        String[] urls = new String[]{
                "http://www.url.com/1",
                "http://www.url.com/2",
                "http://www.url.com/3"
            };
        crawler.crawl(urls);
        try {
            Thread.sleep(1000 * 60 * 15); // without this the unit test exits too early
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

  // the rest of the class definition

}

应用于依赖项的测试范围意味着它在编译类路径上不可用。这样,您发布的代码不依赖于测试代码。可以找到更完整的解释。如果您正在构建的项目仅用于测试,则应删除scope标记,使其采用默认范围compile。但总的来说,从src构建的发布代码不应该依赖于JUnit,一个测试库,这是正确的。

是的,这很有道理,我甚至没有注意到那个小标记。我有权复制/粘贴依赖项。非常感谢。:)
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();
public class Crawler() {

    @Test
    public void runAsTest() {
        Crawler crawler = new Crawler();
        String[] urls = new String[]{
                "http://www.url.com/1",
                "http://www.url.com/2",
                "http://www.url.com/3"
            };
        crawler.crawl(urls);
        try {
            Thread.sleep(1000 * 60 * 15); // without this the unit test exits too early
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

  // the rest of the class definition

}
public class Crawler() {

    public static void main(String[] args) {
        Crawler crawler = new Crawler();
        String[] urls = new String[]{
                "http://www.url.com/1",
                "http://www.url.com/2",
                "http://www.url.com/3"
            };
        crawler.crawl(urls);
        try {
            Thread.sleep(1000 * 60 * 15); // without this the unit test exits too early
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

  // the rest of the class definition

}