Kotlin: исключение из-за отсутствия драйвера при запуске jar
Я пытаюсь подключить приложение kotlin, основанное на gradle, к базе данных mssql (через модули hikari & hibernate fw). Когда я запускаю приложение kotlin в IDE (intellij Idea), все работает нормально, но когда я компилирую его с помощью gradlew, оно выдает исключение, что в нем отсутствует драйвер, и я не знаю, как встроить его в jar. Я попробовал поискать в Google, нашел кое-что о добавлении драйвера в classpath jar, но не повезло. Попробовал группу компиляции драйвера : 'net.sourceforge.jtds', name: 'jtds', version: '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 ответ
Хорошо, нашел решение, как упомянул @Hannes, вам нужно явно создать экземпляр драйвера, например Class.forName("net.sourceforge.jtds.jdbc.Driver").newInstance()
в вашем приложении. Более подробная информация здесь: http://jtds.sourceforge.net/faq.html