1

So I have multi-project based Grandle build where I use Kotlin as primary language. Say default project is named as app which has both src/main/kotlin and src/main/resources directories. The werid thing is I can't access resources from the code even if Gradle sets it to the correct one. I was comparing it to the single-project build (the one that has src as main source directory) and when I run:

javaClass.getResource("./")

Both projects point to the same directory placed at build/classes/kotlin/main/. But when I run:

javaClass.getResource("test.txt")

Single-project points to build/resources/test.txt (which is as expected) but getting null in app of multi-project build. Of course file exists in both places

Did I miss some configuration in global build.gradle or project's build.gradle or something else?

Project structure and some important files parts:

+ app
| + src
| | + main
| | | + kotlin
| | | | - Main.kt
| | | + resources
| | | | - test.txt
| - build.gradle
- build.gradle
- settings.gradle

settings.gradle

include 'app'

build.gradle

plugins {
    id 'org.jetbrains.kotlin.jvm' version '1.3.72'
}

version '0.0.1'

subprojects {
    apply plugin: 'java'
    apply plugin: 'org.jetbrains.kotlin.jvm''

    repositories {
        mavenCentral()
        jcenter()
    }

    dependencies {
        compile "org.jetbrains.kotlin:kotlin-stdlib"
        compile "org.reactfx:reactfx:1.4.1"
    }
}

app/build.gradle

plugins {
    id 'application'
    id 'org.openjfx.javafxplugin' version '0.0.9'
}

dependencies {
    compile "org.jetbrains.kotlin:kotlin-reflect:1.3.72"

    testCompile group: 'junit', name: 'junit', version: '4.12'
}

javafx {
    version = "11"
    modules = ['javafx.controls', 'javafx.graphics', 'javafx.fxml']
}

mainClassName = 'MainApp.MainApp'

MainApp.kt

class MainApp {
  companion object {
    @JvmStatic
    fun main(args: Array<String>) {
      javaClass.getResource("test.txt") // this gives null
    }
  }
}
ThaFog
  • 493
  • 3
  • 15
  • Post relevant code please. An [MRE](https://stackoverflow.com/help/minimal-reproducible-example) ideally. – jannis Aug 26 '20 at 22:37
  • Done, @jannis. Hope it's enough – ThaFog Aug 26 '20 at 22:55
  • Just tried your code and it works for me. Although I had to change `mainClassName = 'MainApp.MainApp'` to `mainClassName = 'MainApp'` and remove the extra quote at the end of `apply plugin: 'org.jetbrains.kotlin.jvm''`. Tested with Gradle v6.5. – jannis Aug 26 '20 at 23:27
  • Weird stuff then. I've found out that actually `javaClass.classLoader.getResource` gives me exactly what I want i.e. the file I'm looking for. Still don't know why `javaClass.getResource` can't reach the file @jannis – ThaFog Aug 27 '20 at 00:48
  • Without an MRE it's hard to tell what's wrong here. – jannis Aug 27 '20 at 11:50

0 Answers0