Ant和Junit测试java.lang.ClassNotFoundException

Ant和Junit测试java.lang.ClassNotFoundException,java,linux,ant,junit,Java,Linux,Ant,Junit,我遇到了一个ant build.xml文件的问题,该文件为我提供了一个java.lang.ClassNotFoundException。我能够在windows上很好地运行它,但是当我将它移植到Linux虚拟机时,我得到了一个异常 <project name="OBJECT" default="compile" > <!-- Properties for the directories of .java These are the locations the the source

我遇到了一个ant build.xml文件的问题,该文件为我提供了一个java.lang.ClassNotFoundException。我能够在windows上很好地运行它,但是当我将它移植到Linux虚拟机时,我得到了一个异常

<project name="OBJECT" default="compile" >
<!--
Properties for the directories of .java
These are the locations the the source files
These are the locations of the .jar dependancy
 -->
<property name="src.dir" location="src"/>
<property name="src.java.dir" location="${src.dir}/csc439"/>
<property name="src.test.dir" location="${src.dir}/test"/>
<property name="lib.dir" location="lib"/>

<!--
Properties for the directories of .class
This gets deleted when ant clean is run
 -->
<property name="target.dir" location="target"/>
<property name="target.classes.java.dir" location="${target.dir}/classes/java"/>
<property name="target.classes.test.dir" location="${target.dir}/classes/test"/>

<!--Properties for the report directory-->
<property name="target.report.dir" location="${target.dir}/report"/>
<!-- 
compile.java 
Creates a directory for the .class files of the Java files for the file being tested
Compiles the files and places the .class into the java file created
Imports the necissary .jar files from the lib directory
-->
<target name="compile.java">
        <mkdir dir="${target.classes.java.dir}"/>
        <javac includeantruntime="true" destdir="${target.classes.java.dir}">
            <src path="${src.java.dir}"/> 
            <classpath> 
                <pathelement location="${target.classes.java.dir}"/>
                    <pathelement location="${lib.dir}"/>
                <fileset dir="${lib.dir}"> 
                    <include name="*.jar"/>
                </fileset> 
            </classpath> 
        </javac>
    </target>
<!-- 
compile.test 
Depends on compile.java to complete first
Creates a directory for the .class files of the Test files
Compiles the files and places the .class into the test file created
-->
<target name="compile.test" depends="compile.java">
    <mkdir dir="${target.classes.test.dir}"/>
    <javac includeantruntime="true" destdir="${target.classes.test.dir}">
        <src path="${src.test.dir}"/>
        <classpath>
            <pathelement location="${target.classes.java.dir}"/>
                <pathelement location="${lib.dir}"/>
            <fileset dir="${lib.dir}"> 
                <include name="*.jar"/>
            </fileset> 
        </classpath>
    </javac>
</target>
<!-- 
compile
This the the default
Depends on compile.java, and compile.test
-->
<target name="compile" depends="compile.java,compile.test"/>
<!-- 
test
Depends on compile
Creates the report file
Runs the JUnit test TestCacheSuite in the test file in the test .class directory
--> 
<target name="test" depends="compile">
    <mkdir dir="${target.report.dir}"/>
    <junit printsummary="yes" haltonerror="yes" haltonfailure="yes" fork="yes">
        <formatter type="plain" usefile="false"/>
            <formatter type="xml"/>
        <test name="test.TestMediaPlayer" todir="${target.report.dir}"/>
        <classpath>
            <pathelement location="${target.classes.java.dir}"/>
            <pathelement location="${target.classes.test.dir}"/>
        </classpath>
    </junit>
</target>   
<!-- 
report
Depends on test
Creates the file for html documents
Depends on Test
Creates a Junit report 
--> 
<target name="report" depends="test">
    <mkdir dir="${target.report.dir}/html"/>
    <junitreport todir="${target.report.dir}">
        <fileset dir="${target.report.dir}">
            <include name="TEST-*.xml"/>
        </fileset>
        <report todir="${target.report.dir}/html"/>
    </junitreport>
</target>
<!--
clean
Deletes the target directory
This file contains all of the .class files
This file contains all of the reports
-->
<target name = "clean">
    <delete dir = "${target.dir}"/>
</target>
使用完全相同的构建文件,我让它在windows上运行良好

我在windows上的命令行中运行得很好,并且使用Eclipse两者都可以完美地工作

从我读到的所有内容来看,我需要检查类路径和路径变量。 我做过这件事,但一定是做错了什么。我不明白为什么在一个操作系统中运行的构建信息相同,而在另一个操作系统中运行的信息不同


任何帮助都将不胜感激

没有太多的信息可供参考,但为了冒险猜测,我认为您需要将
${lib.dir}
作为
添加到junit调用的类路径中。


<echoproperties/>

在这种情况下,这是你最好的朋友。如果它在windows上运行正常,但在linux上不运行,那么它一定是文件分隔符有问题(但是在windows上使用ant/works也有问题),或者只是您的当前目录不同(您在任何地方都使用相对路径)。如果您有不同的当前目录,您将在
echoproperties
的输出中看到它作为
“user.dir”
。您还可以将
javac
设置为
verbose=“true”
,然后您可以将脚本的windows输出与linux输出进行比较。

对于我来说,出现此问题是因为我的测试文件使用了内部类。这在eclipse中被编译为一个单独的类文件,您需要classname.class和classname$1.class才能让ant运行测试。

尝试将junit jar位置添加到classpath元素中。我不知道这对我有什么作用

大概

       <classpath>
            <pathelement location="path/to/junit/jar"/>
            <pathelement location="${target.classes.java.dir}"/>
            <pathelement location="${target.classes.test.dir}"/>
        </classpath>


请检查目录的属性,两个操作系统中的斜杠不同。另外,检查编译的类是否真的在ant期望的目录中。其他事情可能是文件权限,但我猜这会导致“编译”目标在mkdir期间失败。目录应该在它们所在的位置。正如我所说,它在windows中运行良好,但在linux中运行不好。斜杠应该没问题,因为它们是相对目录,而构建不会像您所说的那样工作。问题在于ant构建文件中的测试。lib只需要编译。
       <classpath>
            <pathelement location="path/to/junit/jar"/>
            <pathelement location="${target.classes.java.dir}"/>
            <pathelement location="${target.classes.test.dir}"/>
        </classpath>