gpt4 book ai didi

java - Ant 和 Junit 测试 java.lang.ClassNotFoundException

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:11:06 31 4
gpt4 key购买 nike

我遇到了一个 ant build.xml 文件的问题,它给我一个 java.lang.ClassNotFoundExcpetion。我可以在 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>

这是我在 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 变量。我已经这样做了,但我一定做错了什么。我不明白为什么使用相同的构建信息可以在一个操作系统中运行,而不是另一个操作系统。

任何帮助将不胜感激

最佳答案

没有大量的信息可以继续,但为了冒险猜测,我想说你需要添加你的 ${lib.dir}作为 <pathelement>在你的 junit 调用的类路径中。

关于java - Ant 和 Junit 测试 java.lang.ClassNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13482439/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com