Kotlin:运行jar时缺少驱动程序的异常

时间:2018-01-28 18:33:42

标签: jar kotlin hikaricp jtds

我正在尝试将基于gradle的kotlin app连接到mssql数据库(通过模块hikari& hibernate fw)。 当我在IDE(intellij Idea)中运行kotlin应用程序时,一切正常,但是当我通过gradlew编译它时,它会抛出异常,它缺少驱动程序而我不知道如何将其嵌入到jar中。我试过谷歌搜索,发现一些关于添加驱动程序到jar的类路径,但没有运气。 尝试过驱动程序编译组:' net.sourceforge.jtds',名称:' jtds',版本:' 1.3.1' 在依赖项中,但也没有运气。

具有数据库处理程序的模块具有build.gradle

apply plugin: 'java'
apply plugin: 'kotlin'

sourceCompatibility = 1.8
targetCompatibility = 1.8

ext {
    kotlinVersion = '1.1.50'
    jUnitVersion = '4.12'
    jacksonVersion = '2.8.6'
    networkntVersion = '0.1.7'
    jsoupVersion = '1.10.3'
    slf4jVersion = '1.7.21'
    elasticSearchVersion = '5.6.5'
    hikariCpVersion = '2.6.1'
    hibernateVersion = '5.2.10.Final'
    redisVersion = '2.9.0'
    mysqlVersion = '6.0.6'
    jtdsVersion = '1.3.1'
    rabbitMqVersion = '4.2.0'
}

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}
/* BUILDSCRIPT */

buildscript {
    ext.kotlinVersion = '1.1.50'

    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
        classpath "org.jetbrains.kotlin:kotlin-noarg:$kotlinVersion"
    }
}

repositories { mavenCentral() }

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlinVersion"
    testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlinVersion"
    testCompile group: 'junit', name: 'junit', version: "$jUnitVersion"

    // kotlin-reflect as explicit dependency because of compilation errors in gradle
    compile "org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion"

    // https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core
        compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: "$jacksonVersion"

    // https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: "$jacksonVersion"

    // https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: "$jacksonVersion"

    // https://mvnrepository.com/artifact/com.fasterxml.jackson.module/jackson-module-kotlin
    compile group: 'com.fasterxml.jackson.module', name: 'jackson-module-kotlin', version: "$jacksonVersion"

    // https://mvnrepository.com/artifact/com.fasterxml.jackson.datatype/jackson-datatype-jsr310
    compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-jsr310', version: "$jacksonVersion"

    // https://mvnrepository.com/artifact/com.fasterxml.jackson.datatype/jackson-datatype-jdk8
    compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-jdk8', version: "$jacksonVersion"

    // https://mvnrepository.com/artifact/com.fasterxml.jackson.dataformat/jackson-dataformat-xml
    compile group: 'com.fasterxml.jackson.dataformat', name: 'jackson-dataformat-xml', version: "$jacksonVersion"

    // json schema validator (based on jackson)
    compile group: 'com.networknt', name: 'json-schema-validator', version: "$networkntVersion"

    // html parser
    compile "org.jsoup:jsoup:$jsoupVersion"

    // logging
    compile group: 'org.slf4j', name: 'slf4j-api', version: "$slf4jVersion"
    compile group: 'org.slf4j', name: 'slf4j-log4j12', version: "$slf4jVersion"

    // ckf.upe.common.elastic

    // https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch
    // https://www.elastic.co/guide/en/elasticsearch/client/java-api/5.6/transport-client.html
    compile group: 'org.elasticsearch.client', name: 'transport', version: "$elasticSearchVersion"

    // ckf.upe.common.database

    // connection pool
    // https://github.com/brettwooldridge/HikariCP
    compile group: 'com.zaxxer', name: 'HikariCP', version: "$hikariCpVersion"

    // hibernate
    compile group: 'org.hibernate', name: 'hibernate-core', version: "$hibernateVersion"
    // hibernate-hikaricp
    // https://mvnrepository.com/artifact/org.hibernate/hibernate-hikaricp
    compile group: 'org.hibernate', name: 'hibernate-hikaricp', version: "$hibernateVersion"

    // https://mvnrepository.com/artifact/redis.clients/jedis
    // https://github.com/xetorthio/jedis
    compile group: 'redis.clients', name: 'jedis', version: "$redisVersion"
}
运行jar文件时抛出的

异常是:

Exception in thread "main" org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:271)
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:233)
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
        at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:51)
        at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:242)
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
        at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.handleTypes(MetadataBuildingProcess.java:352)
        at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:111)
        at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
        at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418)
        at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:691)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:726)
        at ckf.upe.application.App.getSessionFactory(App.kt:104)
        at ckf.upe.fischer.database.MappingRepository.sessionFactory(MappingRepository.kt:13)
        at ckf.upe.common.sql.AbstractRepository.withSession(AbstractRepository.kt:21)
        at ckf.upe.mapping.service.MappingService.loadMapping(MappingService.kt:33)
        at ckf.upe.application.upe.MainKt.main(main.kt:41)
Caused by: org.hibernate.HibernateException: java.lang.RuntimeException: Failed to get driver instance for jdbcUrl=jdbc:jtds:sqlserver://devsql2.fischer:1433/CKFischer_Mapping_PROD;useLOBs=false
        at org.hibernate.hikaricp.internal.HikariCPConnectionProvider.configure(HikariCPConnectionProvider.java:63)
        at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94)
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:242)
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210)
        at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.buildJdbcConnectionAccess(JdbcEnvironmentInitiator.java:145)
        at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:66)
        at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35)
        at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:88)
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:259)
        ... 18 more
Caused by: java.lang.RuntimeException: Failed to get driver instance for jdbcUrl=jdbc:jtds:sqlserver://devsql2.fischer:1433/CKFischer_Mapping_PROD;useLOBs=false
        at com.zaxxer.hikari.util.DriverDataSource.<init>(DriverDataSource.java:88)
        at com.zaxxer.hikari.pool.PoolBase.initializeDataSource(PoolBase.java:323)
        at com.zaxxer.hikari.pool.PoolBase.<init>(PoolBase.java:114)
        at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:105)
        at com.zaxxer.hikari.HikariDataSource.<init>(HikariDataSource.java:72)
        at org.hibernate.hikaricp.internal.HikariCPConnectionProvider.configure(HikariCPConnectionProvider.java:59)
        ... 26 more
Caused by: java.sql.SQLException: No suitable driver
        at java.sql.DriverManager.getDriver(Unknown Source)
        at com.zaxxer.hikari.util.DriverDataSource.<init>(DriverDataSource.java:81)
        ... 31 more

1 个答案:

答案 0 :(得分:1)

好的,找到@Hannes提到的解决方案,您需要在应用中明确地实例化Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance()之类的驱动程序。更多信息: jtds.sourceforge.net/faq.html