39365

ClassNotFoundException - when running ant-junit

Question:

I'm trying to create a Proof of concept for the actual application, The task is to run ant-junit tests without adding stuff to the global environment. I've achieved it so far in ant, but, I'm stuck with the following exception. ClassTest.java is the java class that has sample unit testcase. (Junit 3.0 style). I'm not sure why ant-junit does not find the class inspite of mentioning the paths in the batchrun task.

<strong>project structure</strong>

<img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/DuaMq.png" data-original="https://i.stack.imgur.com/DuaMq.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

I get the following exception when running ant-junit task.

Testsuite: ClassTest Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec Caused an ERROR ClassTest java.lang.ClassNotFoundException: ClassTest at java.lang.ClassLoader.loadClass(ClassLoader.java:247) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:249) at org.eclipse.ant.internal.launching.remote.EclipseSingleCheckExecutor.executeTargets(EclipseSingleCheckExecutor.java:30) at org.eclipse.ant.internal.launching.remote.EclipseDefaultExecutor.executeTargets(EclipseDefaultExecutor.java:32) at org.eclipse.ant.internal.launching.remote.InternalAntRunner.run(InternalAntRunner.java:424) at org.eclipse.ant.internal.launching.remote.InternalAntRunner.main(InternalAntRunner.java:138)

My ant build.xml file is

<project name="Java project build" default="build"> <property name="project.local.directory" value="C:\Users\usrpao\workspace\Ant" /> <property name="src.path" value="${project.local.directory}/src" /> <property name="lib.path" value="${project.local.directory}/lib" /> <property name="dest.path" value="${project.local.directory}/target" /> <property name="junit.output.dir" value="${project.local.directory}/junit" /> <path id="classpath.test"> <fileset dir="lib"> <include name="**/*.jar" /> </fileset> </path> <!--<taskdef name="junit" classname="org.apache.tools.ant.taskdefs.optional.junit.JUnitTask"> <classpath refid="classpath.test" /> </taskdef> --> <path id="MyProject.classpath"> <pathelement location="lib/ant-junit.jar" /> <pathelement location="lib/junit.jar" /> <pathelement location="bin/*.*"/> </path> <path id="lib.classpath"> <pathelement location="lib/ant-junit.jar" /> <pathelement location="lib/junit.jar" /> </path> <target name="build" depends="clean"> <javac srcdir="${src.path}" destdir="${dest.path}" classpathref="lib.classpath"> </javac> <antcall target="test"> </antcall> </target> <target name="clean"> </target> <target name="test"> <pathconvert property="testoutput" refid="classpath.test" /> <echo>Path = ${testoutput}</echo> <mkdir dir="${junit.output.dir}" /> <junit haltonerror="false" showoutput="true"> <classpath refid="MyProject.classpath"> </classpath> <batchtest todir="${junit.output.dir}"> <formatter type="plain" /> <fileset dir="${src.path}/com/ant/test"> <include name="**/*Test*.java" /> </fileset> </batchtest> </junit> </target>

Answer1:

Your classpath is referencing the bin directory while the classes are actually under the target directory. You should change the MyProject.classpath reference to include ${project.local.directory}/target:

<path id="MyProject.classpath"> <pathelement location="lib/ant-junit.jar" /> <pathelement location="lib/junit.jar" /> <dirset dir="${project.local.directory}"> <include name="target"/> </dirset> </path>

In addition, you should change the fileset element inside the junit task by removing the folders corresponding to the package name of the test class, otherwise you will still get a ClassNotFoundException:

<junit haltonerror="false" showoutput="true"> <classpath refid="MyProject.classpath"> </classpath> <batchtest todir="${junit.output.dir}"> <formatter type="plain" /> <fileset dir="${src.path}"> <!-- remove com/ant/test corresponding to package name --> <include name="**/*Test*.java" /> </fileset> </batchtest> </junit>

Answer2:

From your folder structure it is evident that the class ClassTest.class is in target folder. so in class-path you should include following:

<pathelement path="target"/>

Hope this helps.

Recommend

  • Sencha touch production build errors
  • JBOSS 7 MYSQL missing dependencies error
  • Apache POI, Excel 2007+ XML, and OSGI
  • Hadoop 2.2.0 mapreduce job not running after upgrading from hadoop 1.0.4
  • ClassNotFoundException dependent on JDK7 update version only?
  • ClassNotFoundException when running ant junit
  • Why the Class cannot be seen in its source file Java
  • Java error using JARs, java.lang.NoClassDefFoundError: javax/mail/Store
  • Tomcat JDBC MySQL ClassNotFoundException
  • Class.forname(“name”).newInstance() vs name.class.newInstance : Difference in usage perspective
  • java.lang.NoClassDefFoundError: org/springframework/beans/factory/Aware
  • Grails 2 app wont deploy on JBOSS: BeanCreationException
  • No dhtreader in java.library.path
  • ClassNotFoundException during Deserialization of a just-serializaed class
  • java.lang.ClassFormatError: Illegal field modifiers in class
  • Upload image and information from textfields in the same servlet
  • Cannot find class org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguratio
  • java.lang.IncompatibleClassChangeError
  • Hadoop NoSuchMethodError apache.commons.cli
  • Beginner in SQLite
  • creating instance of object using reflection , when the constructor takes an array of strings as arg
  • Java: can you cast Class into a specific interface?
  • Spring security and special characters
  • Trying to switch camera back to front but getting exception
  • Cannot Parse HTML Data Using Android / JSOUP
  • Free memory of cv::Mat loaded using FileStorage API
  • Angular 2 constructor injection vs direct access
  • Java static initializers and reflection
  • unknown Exception android
  • Programmatically clearing map cache
  • Observable and ngFor in Angular 2
  • Unable to use reactive element in my shiny app