Search code examples
javadatabase-connectionnoclassdeffounderrordbunit

why am i getting dbunit noclassdeffounderror databaseconnection?


in a helper class to my junit test classes, i create a new DataBaseconnection from dbunit to my h2databaseconnection:

IDatabaseConnection dBUnitConnection = new DatabaseConnection(h2con);

and try to run it with "dbunit-2.5.0.jar" in my classpath, i get the following errormessage:

java.lang.NoClassDefFoundError: Could not initialize class org.dbunit.database.DatabaseConnection

i don't know whats going wrong. I compile and run it with eclipse which works, but i want to run the junit test from command line so i try the

java -cp ".;./../libs/*;./libs/dbunit-2.5.0.jar;" org.junit.runner.junitcore testclass

which should work...


Solution

  • Your classpath may be incomplete, it looks like your database driver jar is missing. (A NoClassDefFoundError can also be caused by a problem while initializing a class, e.g. an exception, this is different from a ClassNotFoundException).

    Check your classpath against your Java build path in Eclipse.

    Edit:

    Moved my comment, which pointed to the real cause to the answer:

    Having a look at the source of org.dbunit.database.DatabaseConnection, it could also be a missing slf4j jar. dbunit uses slf4j for logging and tries to initialize the slf4j classes when the DatabaseConnection is initialized.