简体   繁体   中英

Apache Ant: NoClassDefFoundError when using Junit

I just can't seem to get my junit tests run by Ant. Here's my setup:

build.xml http://pastebin.com/c7n4cdJ6

What am I trying to do? I run ant main or ant package respectively. The compile target compiles my code. First the classes of my webapp and afterwards my test classes:

  1. javac : source: webapp java files, destination: ${web.classes.dir}
  2. javac : source: test java files, destination: ${web.testclasses.dir}

For dependency management I use Apache Ivy, which is working fine.

So let's take a look at my junit target. You can see that the classpath is defined in junit.classpath . It contains:

  1. My Ivy dependencies with the test scope: <path refid="test.path"/>
  2. The compiled class files of my tests: <pathelement location="${web.testclasses.dir}"/>
  3. The compiled class files of my webapp: <pathelement location="${web.classes.dir}"/>
  4. Also it contains a dirset of my compiled test classes (you see I was desperate).

So when I run the ant script it compiles well, the class files are all there. But when it comes to junit target and the first test is being executed, I get this error in the console:

    [junit] Running CreateStopwordSqliteDBTest
    [junit] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0 sec

BUILD FAILED

In the result xml file I get further information:

  <error message="CreateStopwordSqliteDBTest (wrong name: de/mycompany/myproduct/CreateStopwordSqliteDBTest)" type="java.lang.NoClassDefFoundError">
    java.lang.NoClassDefFoundError: CreateStopwordSqliteDBTest (wrong name: de/mycompany/myproduct/CreateStopwordSqliteDBTest)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    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:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:191)
</error>

NoClassDefFoundError tells me that the class could be found at compile time but not on runtime. Please note that the affected class is the actual JUnit test. Why can't it be found even though I made sure it is part of my junit classpath?

Fyi: When I remove the lines 131 - 133 from my build.xml (the dirset in my junit classpath), I would get a ClassNotFoundException :

  <error message="CreateStopwordSqliteDBTest" type="java.lang.ClassNotFoundException">
    java.lang.ClassNotFoundException: CreateStopwordSqliteDBTest
    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:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:191)
</error>

Could you help me out here?

The source directories are wrong. Here are the correct properties:

<property name="src.dir" location="${basedir}/src" />
<property name="testsrc.dir" location="${basedir}/testsrc" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM