2012-11-20 132 views
2

我在与同时给我一个java.lang.ClassNotFoundExcpetion蚂蚁的build.xml文件有问题。我能够在Windows上运行它,但是当我将它移植到Linux虚拟机时,我得到了异常。Ant和JUnit测试抛出java.lang.ClassNotFoundException

<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> 

这是我得到的,而在Linux上运行它的错误。

[junit] Running test.MockObject 
[junit] Testsuite: test.MockObject 
[junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec 
[junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec 
[junit] 
[junit]  Caused an ERROR 
[junit] test.MockObject 
[junit] java.lang.ClassNotFoundException: test.MockObject 
[junit]  at java.net.URLClassLoader$1.run(URLClassLoader.java:366) 
[junit]  at java.net.URLClassLoader$1.run(URLClassLoader.java:355) 
[junit]  at java.security.AccessController.doPrivileged(Native Method) 
[junit]  at java.net.URLClassLoader.findClass(URLClassLoader.java:354) 
[junit]  at java.lang.ClassLoader.loadClass(ClassLoader.java:423) 
[junit]  at java.lang.ClassLoader.loadClass(ClassLoader.java:356) 
[junit]  at java.lang.ClassLoader.loadClass(ClassLoader.java:356) 
[junit]  at java.lang.Class.forName0(Native Method) 
[junit]  at java.lang.Class.forName(Class.java:264) 
[junit] 

与完全相同的构建文件,我让它在Windows上运行良好。

我已经从Windows命令行,以及使用Eclipse工作都完美运行良好。

,从我读它的一切说,我需要检查CLASSPATH和PATH变量。 我已经这样做了,但是我一定在做错事。我不明白为什么有相同的构建信息,它会在一个操作系统中运行,而不是另一个。

任何帮助,将不胜感激

+0

请检查您的目录的属性,斜杠是在两个不同的操作系统。另外,检查编译的类是否真的在ant期望的目录中。其他的东西可能是文件权限,但我想在mkdir期间会导致'compile'target失败。 –

+0

这些目录应该在他们所在的位置。就像我说的,它在Windows中运行良好,而不是Linux。斜线应该很好,因为它们是相对的目录,建筑物不会像你所说的那样工作。 – RGG

回答

0

没有成吨的信息去,而是要猜的话,我会说你需要你的${lib.dir}中的类路径添加为<pathelement>你的junit调用。

+0

问题是在ant构建文件中进行测试。 lib只需要编译。 – RGG

0
<echoproperties/> 

这是你在这种情况下最好的朋友。如果它在Windows上运行良好,但不能在Linux上运行,那么它必须与文件分隔符有一些问题(但是也可以在Windows上使用ant/works),或者只是具有不同的当前目录(您在每个地方都使用相对路径) 。如果您有不同的当前目录,您将在echoproperties的输出中看到它为"user.dir"。您还可以设置javacverbose="true",然后你可以比较窗口输出脚本与Linux输出。

0

对我来说,这个问题的出现是因为我的测试文件中使用一个内部类。这在eclipse中被编译为一个独立的类文件,你将需要classname.class和classname $ 1.class来运行你的测试。

0

尝试增加JUnit的JAR位置到类路径中的元素。我不知道如何,但这对我有效!

喜欢的东西,

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