H2数据库org.h2.Driver的ClassNotFoundException [英] H2 database org.h2.Driver ClassNotFoundException
问题描述
我尝试使用Eclipse和ANT运行JUnit测试他们都是complainen的org.h2.Driver类是找不到的。
I try to run JUNIT test with eclipse and ANT they both are complainen that org.h2.Driver class is not found.
我有h2-1.3.164.jar在我的类路径和它的证明这里是从系统属性java.class.path的classpath
I have h2-1.3.164.jar in my classpath and for proof about it here is classpath from system property java.class.path
C:\\ trasferer \\建设;
C:\\ Program Files文件(x86)的\\ IBM \\ IMShared \\插件\\ org.junit4_4.3.1 \\的junit.jar;
C:\\ trasferer \\ lib目录\\ ojdbc6.jar;
C:\\ trasferer \\ lib目录\\ sqljdbc4.jar;
C:\\ trasferer \\ lib中测试\\ h2-1.3.164.jar;
C:\\ trasferer \\ lib中测试\\的junit-4.8.2.jar;
C:\\ trasferer \\ lib中测试\\的log4j-1.2.14.jar;
C:\\ trasferer \\ lib中测试\\ TestUtils.jar;
C:\\ trasferer \\ lib中测试\\ DbUnit的-2.4.8.jar;
C:\\ trasferer \\ lib中测试\\ SLF4J-API-1.5.5.jar;
C:\\ trasferer \\ lib中测试\\ SLF4J-log4j12-1.5.5.jar;
我可以从发现类h2-1.3.164.jar时org.h2.Driver我用eclipse打开它。
运行测试和ANT任务构建类路径
and I can found class org.h2.Driver from h2-1.3.164.jar when I open it with eclipse. Ant task for running test and ANT tasks for "build" classpath
<path id="lib.classpath">
<fileset dir="./lib" includes="*.jar" />
</path>
<path id="test.classpath">
<fileset dir="lib" includes="*.jar" />
<fileset dir="lib-test" includes="*.jar" />
<pathelement location="${dest}" />
<pathelement location="${test-dest}" />
</path>
<target name="run-test" depends="init-test,compile,compile-test">
<property name="classPathToUse" refid="test.classpath" />
<echo>Using classpath: "${classPathToUse}"</echo>
<junit failureProperty="test.failure" fork="on" forkmode="once" outputtoformatters="true" printsummary="on" showoutput="true">
<jvmarg value="-Xmx512m" />
<jvmarg value="-XX:MaxPermSize=128m" />
<jvmarg value="-Duser.timezone=GMT" />
<jvmarg value="-Dexternal-properties=${test.properties}" />
<classpath refid="test.classpath" />
<formatter type="brief" usefile="false" />
<formatter type="xml" />
<batchtest todir="test-results">
<fileset dir="${test-dest}" includes="**/*Test.class" />
</batchtest>
</junit>
<fail message="test failed" if="test.failure" />
</target>
和这里是堆栈跟踪
[junit] ------------- Standard Error -----------------
[junit] log4j:ERROR Could not find value for key log4j.appender.CONSOLE
[junit] log4j:ERROR Could not instantiate appender named "CONSOLE".
[junit] log4j:WARN No appenders could be found for logger (org.dbunit.DatabaseTestCase).
[junit] log4j:WARN Please initialize the log4j system properly.
[junit] ------------- ---------------- ---------------
[junit] Testcase: testBuildLiityntapisteSQL(org.xyz.rigistry.manager.XXServiceImplTest): Caused an ERROR
[junit] org.h2.Driver
[junit] java.lang.ClassNotFoundException: org.h2.Driver
[junit] at java.lang.Class.forName(Class.java:136)
[junit] at org.dbunit.JdbcDatabaseTester.(JdbcDatabaseTester.java:104)
[junit] at org.dbunit.PropertiesBasedJdbcDatabaseTester.(PropertiesBasedJdbcDatabaseTester.java:68)
[junit] at org.dbunit.DBTestCase.newDatabaseTester(DBTestCase.java:70)
[junit] at org.dbunit.DatabaseTestCase.getDatabaseTester(DatabaseTestCase.java:109)
[junit] at org.dbunit.DatabaseTestCase.setUp(DatabaseTestCase.java:151)
[junit] at fi.transferer.junit.AbstractDatabaseTestCase.setUp(Unknown Source)
[junit] at fi.transferer.junit.BasicDatabaseTestCase.setUp(Unknown Source)
所以,能不能有人告诉我是怎么回事吗?
So could someone tell me what is going on, please?
推荐答案
你能张贴Ant构建文件部分,它运行的JUnit?有迹象表明,关闭使用CLASSPATH环境变量的选择....
Could you post the ANT build file section, which runs junit? There are options that switch off use of the CLASSPATH environment variable....
依托的环境变量,使您的构建移植性较差,并在我的经验是管理你的java的依赖非常不灵活的方式。我注意到你已经运行到同一个库的多个版本的问题(JUnit和log4j的两个副本)。
Relying on environment variables makes your build less portable and in my experience is a very inflexible way to manage your java dependencies. I notice you're already running into the problem of multiple versions of the same library (two copies of junit and log4j).
我建议改变你的构建在的build.xml 文件的顶部声明类路径如下:
I would suggest altering your build to declare classpaths at the top of the build.xml file as follows:
<path id="compile.path">
<fileset dir="c:\trasferer\lib" includes="*.jar"/>
</path>
<path id="test.path">
<path refid="compile.path"/>
<fileset dir="c:\trasferer\lib-test" includes="*.jar"/>
</path>
这些类路径就可以在不同的Ant任务中使用,使用 classpathref 属性:
<javac srcdir="${src}"
destdir="${build}"
classpathref="compile.path"
debug="on"
source="1.4"
/>
和JUnit有一个嵌套的classpath能力(用于将已编译的类目录有用):
And junit has a nested classpath capability (useful for adding the compiled classes directory):
<junit printsummary="yes" haltonfailure="yes">
<classpath>
<pathelement location="${build.tests}"/>
<path refid="test.path"/>
</classpath>
<formatter type="plain"/>
<test name="my.test.TestCase" haltonfailure="no" outfile="result">
<formatter type="xml"/>
</test>
<batchtest fork="yes" todir="${reports.tests}">
<fileset dir="${src.tests}">
<include name="**/*Test*.java"/>
<exclude name="**/AllTests.java"/>
</fileset>
</batchtest>
</junit>
这篇关于H2数据库org.h2.Driver的ClassNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!