簡體   English   中英

Ant和Junit測試java.lang.ClassNotFoundException

[英]Ant and Junit test java.lang.ClassNotFoundException

我的螞蟻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變量。 我已經做了,但是我一定做錯了。 我不明白為什么使用相同的構建信息會在一個操作系統中運行,而不是在另一個操作系統中運行。

任何幫助將不勝感激

沒有太多的信息可以繼續進行,但是為了避免猜測,我想您需要在junit調用的類路徑<pathelement> ${lib.dir}作為<pathelement>添加。

<echoproperties/>

在這種情況下,這是您最好的朋友。 如果它可以在Windows上正常運行,但不能在linux上運行,則文件分隔符一定存在一些問題(但是ant /也可以在Windows上運行),或者僅僅是您具有不同的當前目錄(您在任何地方都使用相對路徑) 。 如果您具有不同的當前目錄,您將在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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM