我在user.json
文件夹中有一个名为src/main/resources
的文件。
将我的应用程序打包到jar文件中,并且gradle执行命令
gradle distTar
我无法打开文件并获得像这样的例外
Caused by: java.io.FileNotFoundException: file:/app/api-latest/lib/api-latest.jar!/user.json (No such file or directory)
我尝试以3种方式访问文件,现在都导致了FileNotFoundException:
1:
fun readFileFromResources(filename : String) : File {
return File(filename)
}
2:
fun readFileFromResources(filename : String) : File {
val classLoader = Util::class.java.classLoader
return File(classLoader.getResource(filename).file)
}
3:
fun readFileFromResources(filename : String) : File {
return File("src/main/resources", filename)
}
我已经解压缩生成的.jar并且看到user.json文件确实在包的根目录中。
有人知道这不起作用的原因吗? 我使用的是gradle 4.5.1和Kotlin 1.2.30
我的build.gradle看起来像这样
group 'example'
version 'latest'
buildscript {
ext {
kotlin_version = '1.2.30'
ktor_version = '0.9.0'
logback_version = '1.2.1'
gson_version = '2.8.2'
junit_version = '4.12'
hibernate_version = '5.2.12.Final'
hibernate_validator_version = '6.0.7.Final'
mysql_connector_version = '6.0.4'
h2_version = '1.4.196'
kodein_version = '4.1.0'
jedis_version = '2.9.0'
mockito_version = '2.+'
jaxb_version = '2.3.0'
}
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-noarg:$kotlin_version"
}
}
apply plugin: 'java'
apply plugin: 'kotlin'
apply plugin: 'application'
apply plugin: 'kotlin-jpa'
mainClassName = 'io.ktor.server.netty.DevelopmentEngine'
sourceCompatibility = 1.8
repositories {
mavenCentral()
jcenter()
maven { url "http://dl.bintray.com/kotlin/ktor" }
maven { url "https://dl.bintray.com/kotlin/kotlinx" }
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"
compile "io.ktor:ktor:$ktor_version"
compile "ch.qos.logback:logback-classic:$logback_version"
compile "io.ktor:ktor-server-netty:$ktor_version"
compile group: 'com.google.code.gson', name: 'gson', version: "$gson_version"
compile group: 'org.hibernate', name: 'hibernate-core', version: "$hibernate_version"
compile group: 'org.hibernate.validator', name: 'hibernate-validator', version: "$hibernate_validator_version"
compile group: 'mysql', name: 'mysql-connector-java', version: "$mysql_connector_version"
compile group: 'org.hibernate', name: 'hibernate-c3p0', version: "$hibernate_version"
compile group: 'com.github.salomonbrys.kodein', name: 'kodein', version: "$kodein_version"
compile group: 'redis.clients', name: 'jedis', version: "$jedis_version"
testCompile group: 'junit', name: 'junit', version: "$junit_version"
testCompile group: 'io.ktor', name: 'ktor-server-test-host', version: "$ktor_version"
testCompile group: 'com.h2database', name: 'h2', version: "$h2_version"
testCompile "org.mockito:mockito-core:$mockito_version"
}
kotlin {
experimental {
coroutines "enable"
}
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
答案 0 :(得分:2)
您正在尝试从资源创建文件,该文件必须以不同方式处理
最好的方法是直接从JAR中读取资源。
val r = javaClass.classLoader.getResource("aa").readText()
如果您需要磁盘上的文件,您可以制作一个临时文件,将文件复制到那里并在使用后将其删除。