Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

another java.lang.ClassNotFoundException in ant's junit task

Tags:

java

junit

ant

I can't figure out why I am getting this exception from my ant build.xml file. I checked and everything is in the classpath. Why must this be so complicated?!

I had trouble with Ant in the past and it seems it always is something related to the classpath. I am pointing to junit.jar using both ways: within eclipse: window->preferences->ant->runtime->Ant Home->Add External Jars, and also within the build.xml script. This time Ant is not able to locate my test class in the junit task. Is there something wrong with the way I am pointing to this class?

<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>

I am always getting:

    [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

Apparently, Ant finds junit.jar and attempts to start the test, but why can't it find my test class? I point to the folder with compiled test class. So I know that junit is on Ant's classpath at least, but the ClassNotFound puzzles me.

Any ideas perhaps? Many thanks!

like image 862
denchr Avatar asked Aug 01 '09 03:08

denchr


1 Answers

Are you sure your test class is in the build folder? You're invoking junit in a separate JVM (fork=true) so it's possible that working folder would change during that invocation and with build being relative, that may cause a problem.

Run ant from command line (not from Eclipse) with -verbose or -debug switch to see the detailed classpath / working dir junit is being invoked with and post the results back here if you're still can't resolve this issue.

like image 186
ChssPly76 Avatar answered Sep 27 '22 22:09

ChssPly76