我遇到了一个 ant build.xml 文件的问题,它给了我一个 java.lang.ClassNotFoundExcpetion。我可以在 Windows 上很好地运行它,但是当我将它移植到 Linux vm 时,我得到了异常。
<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 变量。我已经这样做了,但我一定做错了什么。我不明白为什么它会在一个操作系统中运行相同的构建信息,而不是另一个。
任何帮助将不胜感激