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.