Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.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
Ant Junit任务抛出java.lang.ClassNotFoundException_Java_Eclipse_Junit_Ant - Fatal编程技术网

Ant Junit任务抛出java.lang.ClassNotFoundException

Ant Junit任务抛出java.lang.ClassNotFoundException,java,eclipse,junit,ant,Java,Eclipse,Junit,Ant,我有一个简单的Junit测试,它不断抛出java.lang.ClassNotFoundException。下面是文件夹结构 project: |__build.xml |__hamcrest-core-1.3.jar |__junit.jar |__src: | |__Simple.java |__bin: |__Simple.class 请帮助我找到发生此错误的原因。脚本能够找到测试用例,但最后抛出一个错误。请尝试使用以下文件集标记: <fileset dir="bin"

我有一个简单的Junit测试,它不断抛出java.lang.ClassNotFoundException。下面是文件夹结构

project:
|__build.xml
|__hamcrest-core-1.3.jar
|__junit.jar
|__src:
|  |__Simple.java
|__bin:
   |__Simple.class




请帮助我找到发生此错误的原因。脚本能够找到测试用例,但最后抛出一个错误。

请尝试使用以下文件集标记:

<fileset dir="bin">                  
    <include name="**.java" />
</fileset>


在您的情况下:

<classpath>
   <pathelement path="bin" />
   <pathelement location="hamcrest-core-1.3.jar" />
   <pathelement location="junit.jar" />
</classpath>

如前所述

为了使事情更灵活一些,我们添加了一个额外的特性,这使得匹配多个目录级别成为可能。这可用于匹配完整的目录树或目录树中任何位置的文件。为此,必须将“”用作目录名。当**用作模式中目录的名称时,它与零个或多个目录匹配。例如:/test/匹配/test/下的所有文件/目录,例如/test/x.java或/test/foo/bar/xyz.html,但不匹配/xyz.xml

“**”必须用于匹配文件夹,而不能作为通配符来选择部分文件名。 使用以下内容更改您的ant:

<fileset dir="bin">
    <include name="*.class" />
</fileset>

如果要选择bin/下每个目录下的所有“*.class”文件,请使用:

<fileset dir="bin">
    <include name="**/*.class" />
</fileset>

您现在将有一个由测试报告编写器引起的错误。你需要创建一个docs文件夹,一切都会正常工作

最终build.xml将是:

<?xml version="1.0"?>
<project name="Ant-Test" default="main">
    <target name="main">
        <mkdir dir="docs"/>
        <junit printsummary="yes" fork="yes" haltonfailure="no">
            <classpath location="hamcrest-core-1.3.jar" />
            <classpath location="junit-4.12.jar" />
            <classpath path="bin/" />
            <formatter type="plain" />
            <batchtest fork="yes" todir="docs">
                <fileset dir="bin">
                    <include name="**/*.class" />
                </fileset>
            </batchtest>
        </junit>
    </target>
</project>

我不确定
junit
任务是否允许多个
类路径
节点,但无论如何,最好只有一个节点。此外,您的类路径缺少classes目录。正确的声明如下:

<junit ...>
    <classpath>
        <pathelement path="bin"/>
        <pathelement location="hamcrest-core-1.3.jar"/>
        <pathelement location="junit.jar"/>
    </classpath>
    ...
</junit>

...

classpath必须包含“类”文件,而不是java。谢谢!这很有效,必须将bin添加到类路径中,并对文件使用*。。。
<fileset dir="bin">
    <include name="*.class" />
</fileset>
<fileset dir="bin">
    <include name="**/*.class" />
</fileset>
<?xml version="1.0"?>
<project name="Ant-Test" default="main">
    <target name="main">
        <mkdir dir="docs"/>
        <junit printsummary="yes" fork="yes" haltonfailure="no">
            <classpath location="hamcrest-core-1.3.jar" />
            <classpath location="junit-4.12.jar" />
            <classpath path="bin/" />
            <formatter type="plain" />
            <batchtest fork="yes" todir="docs">
                <fileset dir="bin">
                    <include name="**/*.class" />
                </fileset>
            </batchtest>
        </junit>
    </target>
</project>
<junit ...>
    <classpath>
        <pathelement path="bin"/>
        <pathelement location="hamcrest-core-1.3.jar"/>
        <pathelement location="junit.jar"/>
    </classpath>
    ...
</junit>