2014-10-08 166 views
0

我在執行我的JUNIT類的ant腳本時遇到下面的錯誤。我的課程編譯成功。可能是什麼問題?爲什麼它無法找到類文件?請指導。ClassnotfoundException:在爲JUNIT執行Ant腳本時?

CLASSPATH環境變量:

C:\Program Files\Java\jdk1.6.0_34\lib;D:\HR\jboss-5.0.0.GA\lib\endorsed;D:\HR\jboss-5.0.0.GA\lib;D:\HR\jboss-5.0.0.GA\client;D:\HR\apache-ant-1.6.5\lib;C:\Program Files\Java\jdk1.6.0_34\bin 

錯誤:

junit-report: 
    [delete] Deleting directory D:\MyProject\folder\src\html-report 
    [mkdir] Created dir: D:\MyProject\Test\src\html-report 
    [mkdir] Created dir: D:\MyProject\Test\src\html-report\Junit 
    [junit] Testsuite: test.com.folder.service.ejb.session.SL_ServiceTest 
class 
    [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec 

    [junit]  Caused an ERROR 
    [junit] test.com.folder.service.ejb.session.SL_ServiceTest 
    [junit] java.lang.ClassNotFoundException: test.com.folder.service.ejb.sessio 
.SL_ServiceTest.class 
    [junit]  at java.net.URLClassLoader$1.run(URLClassLoader.java:202) 
    [junit]  at java.security.AccessController.doPrivileged(Native Method) 
    [junit]  at java.net.URLClassLoader.findClass(URLClassLoader.java:190) 
    [junit]  at java.lang.ClassLoader.loadClass(ClassLoader.java:306) 
    [junit]  at java.lang.ClassLoader.loadClass(ClassLoader.java:247) 
    [junit]  at java.lang.ClassLoader.loadClass(ClassLoader.java:247) 
    [junit]  at java.lang.Class.forName0(Native Method) 
    [junit]  at java.lang.Class.forName(Class.java:247) 

    [junit] Test test.com.folder.service.ejb.session.SL_ServiceTest 
FAILED 

BUILD SUCCESSFUL 
Total time: 0 seconds 

的build.xml

<project name="MyProject" default="junit-report" basedir="."> 
    <!-- Sets the property variables to point to respective directories --> 
    <property name="junit-xml-dir" value="${basedir}/test-output/junitreports"/> 
    <property name="report-dir" value="${basedir}/html-report" /> 

    <!-- Ant target to generate html report --> 
    <target name="junit-report"> 
    <!-- Delete and recreate the html report directories --> 
    <delete dir="${report-dir}" failonerror="false"/> 
    <mkdir dir="${report-dir}" /> 
    <mkdir dir="${report-dir}/Junit" /> 
    <!-- Ant task to generate the html report. 
    todir - Directory to generate the output reports 

    fileset - Directory to look for the junit xml reports. 

    report - defines the type of format to be generated. 
     Here we are using "noframes" which generates a single html report. 
    --> 
     <junit> 
     <classpath> 
      <pathelement location="./junit-4.8.2.jar"/> 
      <pathelement location="./ant-junit4.jar"/> 
     </classpath> 
     <formatter type="plain" usefile="false" /> <!-- to screen --> 
     <formatter type="plain" /> <!-- to file --> 
     <test name = "test.com.folder.service.ejb.session.SL_ServiceTest" todir="."/> 
    </junit> 
    </target> 
</project> 

回答

0

當Java虛擬機(JVM)嘗試加載特定的類並且在類路徑中找不到指定的類時,拋出ClassNotFoundExceptionClassNotFoundException是一個檢查異常,因此必須在方法或構造函數的throws子句中聲明。

如何應對ClassNotFoundException的

1.Verify that the name of the requested class is correct and that the appropriate .jar file exists in your classpath. If not, you must explicitly add it to your application’s classpath.

2.In case the specified .jar file exists in your classpath then, your application’s classpath is getting overriden and you must find the exact classpath used by your application.

3.In case the exception is caused by a third party class, you must identify the class that throws the exception and then, add the missing .jar files in your classpath.

+0

我用我的classpath環境變量更新了我的問題。請讓我知道我是否錯過了任何東西。 – sTg 2014-10-08 05:09:16

+0

** test.com.folder.service.ejb.sessio .SL_ServiceTest.class **沒有被您的JVM找到,我不認爲它與任何其他jar相關。 – Imran 2014-10-08 05:12:20

+0

你把你的測試類放在你的junit ant任務的類路徑中嗎?希望這個鏈接將有助於http://stackoverflow.com/questions/6984478/junit-test-integrated-with-ant-failed-with-classnotfoundexception – Imran 2014-10-08 05:20:42

1

確保您的項目已經創建的類文件下編譯/類項目的目錄,並修改您的build.xml喜歡這一點;

<project name="MyProject" default="junit-report" basedir="."> 
    <property name="dir.build" value="build"/> 
    <!-- Do something --> 
    <path id="classpath.project"> 
     <pathelement path="${dir.build}"/> 
    </path> 
    <junit> 
     <classpath refid="classpath.project"/> 
     <pathelement location="./junit-4.8.2.jar"/> 
     <pathelement location="./ant-junit4.jar"/> 
     </classpath> 
     <formatter type="plain" usefile="false" /> <!-- to screen --> 
     <formatter type="plain" /> <!-- to file --> 
     <test name = "test.com.folder.service.ejb.session.SL_ServiceTest" todir="."/> 
    </junit> 

    </project> 
相關問題