Jakob Nielsen
Jakob Nielsen

Reputation: 5198

Unable to read file from src/main/resources after gradle distTar in Kotlin

I have a file called user.json in src/main/resources folder. enter image description here

After I package my application into a jar file with gradle executing the command

gradle distTar

I am unable to open the file and getting an Exception like this

Caused by: java.io.FileNotFoundException: file:/app/api-latest/lib/api-latest.jar!/user.json (No such file or directory)

I tried to access to file in 3 ways now all resulting in a 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)
}

I have unpackaged the generated .jar and see the user.json file is indeed in the root of the package.

Does anybody know the reason why this does not work? I am using gradle 4.5.1 and Kotlin 1.2.30

My build.gradle looks like this

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"
}

Upvotes: 1

Views: 5289

Answers (2)

Koch
Koch

Reputation: 594

The url (or path) needs to be a relative path from the repository root.

..meaning, the location of your file or folder from your src folder.

could be "/main/resources/your-folder/" or "/client/notes/somefile.md"

Whatever it is, in order for your JAR file to find it, the url must be a relative path from the repository root.

it must be "src/main/resources/your-folder/" or "src/client/notes/somefile.md"

Now you get the drill, and luckily for Intellij Idea users, you can get the correct path with a right-click on the folder or file -> copy Path/Reference.. -> Path From Repository Root (this is it)

Upvotes: 0

jrtapsell
jrtapsell

Reputation: 7031

Why this doesn't work

You are trying to create a File from a resource, which has to be handled differently

How can I get around this

The best way is to read the resource directly from the JAR.

val r = javaClass.classLoader.getResource("aa").readText()

If you need the file on disk you can make a temporary file, copy the file there and delete it after use.

Upvotes: 8

Related Questions