簡體   English   中英

ant的junit任務中的另一個java.lang.ClassNotFoundException

[英]another java.lang.ClassNotFoundException in ant's junit task

我無法弄清楚為什么我從我的ant build.xml文件中獲取此異常。 我檢查過,一切都在類路徑中。 為什么要這么復雜?!

我過去遇到過Ant問題,似乎它總是與類路徑有關。 我使用兩種方式指向junit.jar:在eclipse:window-> preferences-> ant-> runtime-> Ant Home-> Add External Jars,以及build.xml腳本中。 這次Ant無法在junit任務中找到我的測試類。 我指向這門課的方式有問題嗎?

<target name="init">
    <property name="sourceDir" value="src"/>
    <property name="outputDir" value="build" />
    <property name="junitLocation" value="C:\...\org.junit4_4.3.1\junit.jar" /> 
</target>

<target name="clean" depends="init">
    <delete dir="${outputDir}" />
</target>

<target name="prepare" depends="clean">
    <mkdir dir="${outputDir}" />
</target>

<target name="compile" depends="prepare">
     <javac srcdir="${sourceDir}" destdir="${outputDir}" classpath="${junitLocation}"/>
</target>

<path id="classpath">
   <pathelement location="${outputDir}" />
   <pathelement location="${junitLocation}" />
</path>

<target name="testApplication" depends="compile">
    <echo>Running the junit tests...</echo>
    <junit fork="yes" haltonfailure="yes">
        <test name="my.package.MyTest" />
        <formatter type="plain" usefile="false" />      
        <classpath refid="classpath" />
    </junit>
</target>

我總是得到:

    [junit] Testsuite: my.package.MyTest
    [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
    [junit]     Caused an ERROR
    [junit] my.package.MyTest
    [junit] java.lang.ClassNotFoundException: my.package.MyTest
    [junit]     at java.net.URLClassLoader$1.run(Unknown Source)
    [junit]     at java.security.AccessController.doPrivileged(Native Method)
    [junit]     at java.net.URLClassLoader.findClass(Unknown Source)
    [junit]     at java.lang.ClassLoader.loadClass(Unknown Source)
    [junit]     at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    [junit]     at java.lang.ClassLoader.loadClass(Unknown Source)
    [junit]     at java.lang.ClassLoader.loadClassInternal(Unknown Source)
    [junit]     at java.lang.Class.forName0(Native Method)
    [junit]     at java.lang.Class.forName(Unknown Source)

BUILD FAILED

顯然,Ant找到了junit.jar並試圖開始測試,但為什么不能找到我的測試類呢? 我指向編譯測試類的文件夾。 所以我知道junit至少在Ant的類路徑上,但是ClassNotFound讓我很困惑。

也許有任何想法? 非常感謝!

你確定你的測試類在build文件夾中嗎? 您在一個單獨的JVM(fork = true)中調用junit ,因此工作文件夾可能會在調用期間發生更改,並且build是相對的,這可能會導致問題。

使用-verbose或-debug開關從命令行(而不是從Eclipse)運行ant,以查看正在調用的詳細類路徑/工作目錄junit ,如果仍然無法解決此問題,請將結果發布回此處。

您不需要將JUnit jar添加到類路徑中。 如果ant無法找到它, <junit>任務將無法運行。

我建議嘗試不同的方法來指定類路徑,如http://ant.apache.org/manual/using.html#path中的ant doc中所述; 特別是<classpath path="${srcdir}" />可能有所幫助。

如果沒有,ChssPly76建議從命令行運行ant -debug是最好的選擇。 你會想要在命令提示符窗口中提升緩沖區,因為ant的調試器非常冗長。

暫無
暫無

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

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