java.lang.ClassNotFoundException:' org.h2.jdbcx.JdbcDataSource'

时间:2016-04-11 14:42:53

标签: sql groovy hikaricp

我正在尝试将HikariCPh2数据库一起使用。这是我的班级负责sql

@Singleton
@Log
class SqlPersistence {

    def config
    {
        def props = new Properties()
        new File("build/resources/main/db.properties").withInputStream {
            props.load(it)
        }
        log.info "Loaded properties: $props"
        config = new HikariConfig(props)
    }

    @Lazy def ds = new HikariDataSource(config)
    @Lazy def sql = new Sql(ds)

}

但是当我尝试使用gradle任务运行它时,我收到此错误

Exception in thread "main" java.lang.RuntimeException: java.lang.ClassNotFoundException: 'org.h2.jdbcx.JdbcDataSource'
        at com.zaxxer.hikari.util.UtilityElf.createInstance(UtilityElf.java:89)
        at com.zaxxer.hikari.pool.PoolBase.initializeDataSource(PoolBase.java:293)
        at com.zaxxer.hikari.pool.PoolBase.<init>(PoolBase.java:90)
        at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:101)
        at com.zaxxer.hikari.HikariDataSource.<init>(HikariDataSource.java:71)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
        at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:77)
        at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrapNoCoerce.callConstructor(ConstructorS
ite.java:102)
        at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:57)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:232)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:244)
        at repository.persistence.SqlPersistence.getDs(SqlPersistence.groovy:27)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
        at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:324)
        at org.codehaus.groovy.runtime.metaclass.MethodMetaProperty$GetBeanMethodMetaProperty.getProperty(MethodMetaProperty
.java:73)
        at org.codehaus.groovy.runtime.callsite.GetEffectivePogoPropertySite.getProperty(GtivePogoPropertySite.java:82)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:304)
        at repository.persistence.SqlPersistence.getSql(SqlPersistence.groovy:28)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
        at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:324)
        at org.codehaus.groovy.runtime.metaclass.MethodMetaProperty$GetBeanMethodMetaProperty.getProperty(MethodMetaProperty
.java:73)
        at org.codehaus.groovy.runtime.callsite.GetEffectivePogoPropertySite.getProperty(GetEffectivePogoPropertySite.java:8
2)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:304)
        at repository.persistence.SqlPersistence.selectAll(SqlPersistence.groovy:42)
        at repository.persistence.SqlPersistence$selectAll.call(Unknown Source)
        at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:110)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:122)
        at repository.BookRepository.findAll(BookRepository.groovy:11)
        at repository.BookRepository$findAll.call(Unknown Source)
        at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:110)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:114)
        at service.BookService.findAll(BookService.groovy:13)
        at service.BookService$findAll.call(Unknown Source)
        at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
    org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:110)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:114)
        at script.run(script.groovy:7)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
        at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:324)
        at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1207)
        at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1016)
        at org.codehaus.groovy.runtime.InvokerHelper.invokePogoMethod(InvokerHelper.java:914)
        at org.codehaus.groovy.runtime.InvokerHelper.invokeMethod(InvokerHelper.java:897)
        at org.codehaus.groovy.runtime.InvokerHelper.runScript(InvokerHelper.java:407)
        at org.codehaus.groovy.runtime.InvokerHelper$runScript.call(Unknown Source)
        at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:110)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:130)
        at script.main(script.groovy)
Caused by: java.lang.ClassNotFoundException: 'org.h2.jdbcx.JdbcDataSource'
        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 com.zaxxer.hikari.util.UtilityElf.createInstance(UtilityElf.java:76)
        ... 65 more

我的build.gradle看起来像这样

apply plugin: 'groovy'

repositories {
    jcenter()
}

dependencies {
    compile 'org.codehaus.groovy:groovy-all:2.4.3'
    compile 'com.h2database:h2:1.4.191'
    compile 'com.zaxxer:HikariCP:2.4.5'

    testCompile 'org.spockframework:spock-core:1.0-groovy-2.4'
    testCompile 'junit:junit:4.12'
    testCompile 'cglib:cglib:3.2.1'
}

task runScript(dependsOn: 'compileJava', type: JavaExec) {
    main = 'script'
    classpath = sourceSets.main.runtimeClasspath + sourceSets.main.output
}

为什么它找不到司机? 附:使用sql驱动程序总是存在一些问题......

1 个答案:

答案 0 :(得分:0)

看起来您的script是Java,但您发布的代码段是Groovy。所以我认为这是你如何调用Groovy部分以使H2 lib不在Groovy类路径中的问题,因为如果我将你的代码转换为Java并将它放在script.java中,它可以适用于你的构建。 gradle这个。但它也可能是Groovy类加载问题。当我尝试从Gradle脚本(基本上也是一个Groovy脚本)访问数据库时,我必须执行以下操作才能使其正常工作:

// load JDBC drivers to the Groovy class loader to overcome java.sql.DriverManager quirks
Sql.classLoader.addURL new File('driver.jar').toURI().toURL()

Sql.withInstance('my:connection:string', 'user', 'pass', 'my.jdbc.DriverClass') {
   it.execute 'select * from foo'
}

也许你需要类似的东西。