Can't find resource file when using common module

Hello, I’m developing a full stack application, with three modules: backend, frontend a common. Their names are self explanatory. Using Kotlin 1.2.41

The problem is that when having the common module, using getResource() in any class of the backend, it returns an exception saying the file is not found. Without the common module it works.

The code I’m using:

object ResourceTest {
        val content = javaClass.classLoader.getResource("app.properties").readText()
}

This is the exception:

Exception in thread "main" java.lang.ExceptionInInitializerError
	at com.example.AppKt.main(App.kt:8)
Caused by: java.lang.IllegalStateException: javaClass.classLoader.ge…esource("app.properties") must not be null
	at com.example.ResourceTest.<clinit>(ResourceTest.kt:4)
	... 1 more

This is the folder tree:

my-app
├── backend
|  ├── build.gradle
|  └── src
|     └── main
|        ├── kotlin
|        └── resources <-- app.properties here
├── common
|  ├── build.gradle
|  └── src
|     └── main
|        ├── kotlin
|        └── resources
├── frontend
|  ├── build.gradle
|  └── src
|     └── main
|        ├── kotlin
|        └── resources
└── build.gradle

This is the build.gradle of the backend module, where the resources are stored.

apply plugin: 'kotlin'
apply plugin: 'kotlin-platform-jvm'
apply plugin: 'com.github.johnrengelman.shadow'

buildscript {
    ext {
        exposed_version = '0.10.2'
        h2_version = '1.4.197'
        hikari_version = '3.1.0'
        konfig_version = '1.6.7.0'
        ktor_version = '0.9.2'
        logback_version = '1.2.3'
        shadow_version = '2.0.4'
    }
    dependencies {
        classpath "com.github.jengelman.gradle.plugins:shadow:$shadow_version"
    }
}

repositories {
    maven { url 'https://dl.bintray.com/kotlin/ktor' }
    maven { url 'https://dl.bintray.com/kotlin/exposed' }
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

    implementation "io.ktor:ktor-auth:$ktor_version"
    implementation "io.ktor:ktor-gson:$ktor_version"
    implementation "io.ktor:ktor-html-builder:$ktor_version"
    implementation "io.ktor:ktor-server-netty:$ktor_version"

    implementation "com.h2database:h2:$h2_version"
    implementation "com.zaxxer:HikariCP:$hikari_version"
    implementation "org.jetbrains.exposed:exposed:$exposed_version"

    implementation "ch.qos.logback:logback-classic:$logback_version"
    implementation "com.natpryce:konfig:$konfig_version"

    expectedBy project(':common')
}

configurations {
    implementation {
        exclude group: 'org.jetbrains.kotlin', module: 'kotlin-runtime'
    }
}

sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
compileKotlin {
    kotlinOptions.jvmTarget = '1.8'
}

compileTestKotlin {
    kotlinOptions.jvmTarget = '1.8'
}

kotlin {
    experimental {
        coroutines 'enable'
    }
}

and applied kotlin-platform-jvm and kotlin-platform-js respectively to each module.

Also tried copying the resources to the common module, but doesn’t work. Cleaning the project and invalidate IntelliJ cache.

This happens when debugging or running the app in IntelliJ, when compiling into a fat jar it works.

I don’t know if I’m missing something.

One of the consequences of multiplatform still being experimental. Have a look at the content of your build folders (I assume the resource access is purely restricted to the JVM module/backend). Do the resources end up in the right place? Are they added properly to the IDEA classpath?

Having the same problem right now. It seems that when you build a JVM library using the Kotlin/Multiplatform plugin, the resources are not loaded in the classpath of a consumer Kotlin/JVM library/application. Not sure if the problem relies in the plugin or not or maybe Gradle itself.

Still, if the consumer library/app is a Kotlin/Multiplatform-JVM-target (yeah the confusion is real…) the resources folder is correctly loaded in the classpath at runtime. Any workaround so far? The last post was from 2 years ago!

Also I reported the issue here: https://youtrack.jetbrains.com/issue/KT-38165

1 Like