H2 数据库 org.h2.Driver ClassNotFoundException [英] H2 database org.h2.Driver ClassNotFoundException

查看:55
本文介绍了H2 数据库 org.h2.Driver ClassNotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 eclipse 和 ANT 运行 JUNIT 测试,他们都抱怨找不到 org.h2.Driver 类.

我的类路径中有 h2-1.3.164.jar,为了证明它,这里是来自系统属性 java.class.path 的类路径

<代码>c:\trasferer\build;C:\Program Files (x86)\IBM\IMShared\plugins\org.junit4_4.3.1\junit.jar;c:\trasferer\lib\ojdbc6.jar;c:\trasferer\lib\sqljdbc4.jar;c:\trasferer\lib-test\h2-1.3.164.jar;c:\trasferer\lib-test\junit-4.8.2.jar;c:\trasferer\lib-test\log4j-1.2.14.jar;c:\trasferer\lib-test\TestUtils.jar;c:\trasferer\lib-test\dbunit-2.4.8.jar;c:\trasferer\lib-test\slf4j-api-1.5.5.jar;c:\trasferer\lib-test\slf4j-log4j12-1.5.5.jar;

当我用eclipse打开它时,我可以从h2-1.3.164.jar找到类org.h2.Driver.用于运行测试的 Ant 任务和用于构建"类路径的 ANT 任务

<fileset dir="./lib" includes="*.jar"/></路径><path id="test.classpath"><fileset dir="lib" includes="*.jar"/><fileset dir="lib-test" includes="*.jar"/><pathelement location="${dest}"/><pathelement location="${test-dest}"/></路径><target name="run-test" depends="init-test,compile,compile-test"><property name="classPathToUse" refid="test.classpath"/><echo>使用类路径:"${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="测试失败" if="test.failure"/></目标>

这里是堆栈跟踪

<前><代码>[junit] ------------- 标准错误 -----------------[junit] log4j:ERROR 找不到键 log4j.appender.CONSOLE 的值[junit] log4j:ERROR 无法实例化名为CONSOLE"的 appender.[junit] log4j:WARN 找不到记录器 (org.dbunit.DatabaseTestCase) 的附加程序.[junit] log4j:WARN 请正确初始化 log4j 系统.[junit] ------------- ---------------- ---------------[junit] 测试用例:testBuildLiityntapisteSQL(org.xyz.rigistry.manager.XXServiceImplTest):导致错误[junit] org.h2.Driver[junit] java.lang.ClassNotFoundException: org.h2.Driver[junit] 在 java.lang.Class.forName(Class.java:136)[junit] 在 org.dbunit.JdbcDatabaseTester.(JdbcDatabaseTester.java:104)[junit] 在 org.dbunit.PropertiesBasedJdbcDatabaseTester.(PropertiesBasedJdbcDatabaseTester.java:68)[junit] 在 org.dbunit.DBTestCase.newDatabaseTester(DBTestCase.java:70)[junit] 在 org.dbunit.DatabaseTestCase.getDatabaseTester(DatabaseTestCase.java:109)[junit] 在 org.dbunit.DatabaseTestCase.setUp(DatabaseTestCase.java:151)[junit] 在 fi.transferer.junit.AbstractDatabaseTestCase.setUp(未知来源)[junit] 在 fi.transferer.junit.BasicDatabaseTestCase.setUp(来源不明)

有人能告诉我发生了什么吗?

解决方案

您能否发布运行 junit 的 ANT 构建文件部分?有一些选项可以关闭 CLASSPATH 环境变量的使用......

<小时>

依赖环境变量会降低构建的可移植性,根据我的经验,这是管理 Java 依赖项的一种非常不灵活的方式.我注意到您已经遇到了同一库的多个版本(junit 和 log4j 的两个副本)的问题.

我建议更改您的构建以在 build.xml 文件的顶部声明类路径,如下所示:

<fileset dir="c:\trasferer\lib" includes="*.jar"/></路径><path id="test.path"><path refid="compile.path"/><fileset dir="c:\trasferer\lib-test" includes="*.jar"/></路径>

这些类路径然后可以用于各种 ant 任务,使用 classpathref 属性:

<javac srcdir="${src}"destdir="${build}"classpathref="compile.path"调试=开"来源=1.4"/>

并且 junit 具有嵌套类路径功能(用于添加已编译的类目录):

<类路径><pathelement location="${build.tests}"/><path refid="test.path"/></classpath><formatter type="plain"/><test name="my.test.TestCase"haltonfailure="no" outfile="result"><formatter type="xml"/></测试><batchtest fork="yes" todir="${reports.tests}"><fileset dir="${src.tests}"><include name="**/*Test*.java"/><exclude name="**/AllTests.java"/></文件集></batchtest></junit>

I try to run JUNIT test with eclipse and ANT they both are complainen that org.h2.Driver class is not found.

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\build; C:\Program Files (x86)\IBM\IMShared\plugins\org.junit4_4.3.1\junit.jar; c:\trasferer\lib\ojdbc6.jar; c:\trasferer\lib\sqljdbc4.jar; c:\trasferer\lib-test\h2-1.3.164.jar; c:\trasferer\lib-test\junit-4.8.2.jar; c:\trasferer\lib-test\log4j-1.2.14.jar; c:\trasferer\lib-test\TestUtils.jar; c:\trasferer\lib-test\dbunit-2.4.8.jar; c:\trasferer\lib-test\slf4j-api-1.5.5.jar; c:\trasferer\lib-test\slf4j-log4j12-1.5.5.jar;

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>

and here is stacktrace


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

解决方案

Could you post the ANT build file section, which runs junit? There are options that switch off use of the CLASSPATH environment variable....


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

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>

These classpaths can then be used in the various ant tasks, using the classpathref attribute:

<javac srcdir="${src}"
         destdir="${build}"
         classpathref="compile.path"
         debug="on"
         source="1.4"
  />

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆