ANT 構建失敗並在 html 報告中給出 classNotFoundExceeption (ANT build fails and gives classNotFoundExceeption in html report)


問題描述

ANT 構建失敗並在 html 報告中給出 classNotFoundExceeption (ANT build fails and gives classNotFoundExceeption in html report)

After doing so many trial and errors on "How ANT tag works ?", I decided to write my own custom build xml file for the testcases that was written in Java and integrated with JUnit. Unfortunately my build script failing with "ClassNotFoundException". And i can see the log in generated HTML file that you get to see after running ant build script. 

Please see below

<project name="WebServices integrated with JUnit and generating report with ANT" default="test" basedir="." >

<description> REST Services integration with JUnit </description>



  <!‑‑ set global properties for this build ‑‑>

        <property name="project_name" value="junit"/>
        <property name="src" location="src"/>
        <property name="build" location="bin"/>
        <property name="dist" location="dist"/>
        <property name="lib" location="${user.home}/My Documents/Mr/jars"/>
        <property name="reports" location="reports"/>

  <!‑‑ the names of various distributable files. NOTE: Generating distribution file "target" is not used here‑‑>


  <!‑‑ Delete the ${build} and ${dist} directory trees ‑‑>

  <target name="clean" description="clean up">

        <delete dir="${build}"/>

        <delete dir="${dist}"/>

        <delete dir="${reports}"/>

  </target>


  <!‑‑ Top level targets ‑‑>


  <target name="compile" depends="init" description="compile the source code">

        <javac srcdir="${src}" destdir="${build}">

              <classpath>

                    <fileset dir="${lib}">

                          <include name="**/*.jar"/>

                    </fileset>

              </classpath>

        </javac>

  </target>



  <!‑‑ run your tests ‑‑>


  <target name="run‑tests" depends="compile" description="run your test suite">

        <junit printsummary="yes" haltonfailure="no" showoutput="yes">

              <classpath>

                    <pathelement path="${build}"/>

                    <fileset dir="${lib}">

                          <include name="**/*.jar"/>

                    </fileset>

              </classpath>


              <batchtest fork="yes" todir="${reports}/raw/">

                    <formatter type="xml"/>

                    <fileset dir="${src}/Test/Services" >

                          <exclude name="MyFile.java"/>

                          <include name="**/*.java"/>  // <‑‑‑‑‑‑ IMP***: Here I am saying include .java files that are based at "${src}/Test/Services".

                    </fileset>

              </batchtest>

        </junit>

  </target>


  <!‑‑ generate report on tests ‑‑>



  <target name="test" depends="run‑tests">

        <junitreport todir="${reports}">

              <fileset dir="${reports}/raw/">

                          <include name="TEST‑*.xml"/>

              </fileset>


              <report format="frames" todir="${reports}/html/"/>

        </junitreport>

  </target>



  <target name="init" depends="clean" description="initialize the build envrionment">

              <!‑‑create the time stamp ‑‑>

              <tstamp/>

              <!‑‑ Create directory structure ‑‑>

              <mkdir dir="${build}"/>       //<‑‑‑‑dir for class files

              <mkdir dir="${lib}"/>         //<‑‑‑‑dir for all my libraries

              <mkdir dir="${dist}/lib"/>    //<‑‑‑‑not used

              <mkdir dir="${reports}"/>

              <mkdir dir="${reports}/raw/"/>

              <mkdir dir="${reports}/html/"/>     //<‑‑‑‑ it will have output reports 

  </target>



  <target name="all" depends="clean,test">

  </target>

And I guessed ANT build will pick all the source files (.java) and then it will look for all the class files that are based in build folder and started running them, but then I see "classNotFoundException" in HTML report. please see below log :

CLASS : "getlieninfo"

>   java.lang.ClassNotFoundException: getlieninfo
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:186)

After a while I changed .java to .class in "include" tag of my target called "run‑tests" . The reason for doing this is, I thought ANT is not able to look ".java" files in source folder (src/Test/Services) hence I changed to ".class" and then modified dir attribute value in "fileset" tag to "build" so it may easily look for ".class" in BUILD folder where I will have compiled files stored. But none of my trial and error succeeded and ended up with same "classNotFoundException". 

  

I am not sure what went wrong , can someone help me,  please? 


參考解法

方法 1:

OK guys, Thanks for what ever little help I got from you. I found the problem.

  

I had to change include tag attribute name to "name=/Test/Services/**/*.java"/>"  and modify dir attribute in fileset tag to "${src}" ; see below :

<batchtest fork="yes" todir="${reports}/raw/">

      <formatter type="xml"/>

        <fileset dir="${src}" >
             <exclude name="MyFile.java"/>
                <include name="/Test/Services/**/*.java"/>  //<‑‑‑‑this where i had to modify.

NOTE: Under src directory I have Test/Services folder which has source ".java" files in it. 

        </fileset>
</batchtest>

It solved my problem, but then i don't understand why ant build was not able to identify source files when i gave dir attribute as dir="${src}/Test/Services" that is in fileset tag and kept name attribute in include tag as name="**/*.java" .  My understanding is fileset tag should build the path for for the given dir path , once the path is build include tag will include or look for mentioned source files i.e. ".java" . 

(by MKodMKod)

參考文件

  1. ANT build fails and gives classNotFoundExceeption in html report (CC BY‑SA 3.0/4.0)

#java #automated-tests #build-automation #ant #junit






相關問題

電子郵件地址中帶有 + 字符的 Java 郵件 (Java mail with + character in email address)

如何快速原型化 Java 代碼? (How to quickly prototype Java code?)

如何使用 Maven 在目標(SVN-)服務器上創建 Javadoc? (How to create Javadoc on the target (SVN-) server using Maven?)

為什麼檢查二叉樹有效性的解決方案不起作用? (Why the solution for checking the validity of binary tree is not working?)

Selenium webdriver通過第一個數字找到texy (Selenium webdriver find texy by first digits)

setOnClickListener 沒有在圖像視圖上被調用 (setOnClickListener is not getting called on image view)

繪製多邊形:找不到錯誤 (Drawing Polygon : unable to find error)

半透明 JButton:對像出現在背景中 (Semi-Transparent JButton: Objects appear in Background)

比較同一數組的元素 (Compare elements of the same array)

Java 屏幕截圖小程序 (Java screen capture applet)

Minecraft 1.8.9 Forge Modding 的Java 開發工具包,需要什麼JDK/JRE,代碼是否正確? (Java Development Kit with Minecraft 1.8.9 Forge Modding, What JDK/JRE Is Needed, Is Code Correct?)

java while (resultset.next()) 不返回同一列中的所有數據 (java while (resultset.next()) does not return all data in the same column)







留言討論