1

This is my project structure: enter image description here

This is my module-info:

open module org.client.main {
    exports org.apj.client.app;
    requires javafx.controls;
    requires javafx.base;
    requires javafx.fxml;
    requires spring.context;
    requires spring.web;
    requires java.sql;
    requires spring.beans;
    requires spring.core;
}

This is my parent gradle build file:

subprojects {
    afterEvaluate {
        repositories {
            jcenter()
        }

        compileJava {
            doFirst {
                options.compilerArgs = [
                        '--module-path', classpath.asPath,
                ]
                classpath = files()
            }
        }

        compileTestJava {
            inputs.property("moduleName", moduleName)
            doFirst {
                options.compilerArgs = [
                        '--module-path', classpath.asPath,
                        '--add-modules', 'junit',
                        '--add-reads', "$moduleName=junit",
                        '--patch-module', "$moduleName=" + files(sourceSets.test.java.srcDirs).asPath,
                ]
                classpath = files()
            }
        }

        test {
            inputs.property("moduleName", moduleName)
            doFirst {
                jvmArgs = [
                        '--module-path', classpath.asPath,
                        '--add-modules', 'ALL-MODULE-PATH',
                        '--add-reads', "$moduleName=junit",
                        '--patch-module', "$moduleName=" + files(sourceSets.test.java.outputDir).asPath,
                ]
                classpath = files()
            }
        }
    }
}

This is my client module build file:

plugins {
    id 'java'
}

group 'APJ'
version '1.0-SNAPSHOT'

sourceCompatibility = 11

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'org.springframework', name: 'spring-webmvc', version: '5.1.4.RELEASE'
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

ext.moduleName = 'org.client.main'

I am trying to getResource for FXMLLoader but I simply can not get it to work. I have been struggling for 2 hours and I am really desperate right now. I have tried like every possible combination of file name, every possible location but still it is returning null.

Also I have tried ClassLoader.getSystemClassLoader().getResource("/login.fxml") getClass().getResource("/login.fxml") But it is also not working.

Can someone help me with this ? I would be very grateful.

Druudik
  • 955
  • 1
  • 10
  • 32
  • Gradle and Maven require the resources files, like the FXML files, added to the `src/main/resources` folder. Then `getClass().getResource("/login.fxml")` should work, or `ClientApplication.class.getResource("/login.fxml")`, or `ClientApplication.class.getResource("login.fxml")` if you add it to `src/main/resources/org.apj.client.app/login.fxml`. – José Pereda Jan 22 '19 at 22:03
  • I have tried every possible option still null :( – Druudik Jan 22 '19 at 22:07
  • 2
    You also have to open your module to `javafx.fxml`, adding `opens org.apj.client.app to javafx.fxml` to your module-info class. Also, post the stacktrace you got, it will help finding out the root of the issue. – José Pereda Jan 22 '19 at 22:29
  • Possibly related [Where do resource files go in a Gradle project that builds a Java 9 module?](https://stackoverflow.com/questions/51864473/) – Slaw Jan 22 '19 at 22:37
  • @JoséPereda Opening the package to `javafx.fxml` should only be required to allow reflective access to the controller class (and I don't see a controller in the package). And you give the resource's location via a `URL` which "bypasses" the module system, so encapsulation of the resource shouldn't be an issue. – Slaw Jan 22 '19 at 22:44
  • @Slaw True, but eventually he will use a controller. Anyway we need a stacktrace to figure out what is happening. – José Pereda Jan 22 '19 at 22:49
  • @JoséPereda there is no stack trace needed, the problem is that getClass().getResource("/login.fxml") returns null. Also my whole module is open so I think this should not be a problem. – Druudik Jan 22 '19 at 23:00
  • Ok, can you try the JavaFX plugin? Like in [here](https://github.com/openjfx/samples/blob/master/IDE/IntelliJ/Modular/Gradle/hellofx/build.gradle) (version 0.0.7). – José Pereda Jan 22 '19 at 23:03

2 Answers2

1

I noticed with Java 11, 2 options to make getResource work:

  1. use plugin 'org.openjfx.javafxplugin' version '0.0.7' or
  2. use '--patch-module' in the link provided by Slaw above: Where do resource files go in a Gradle project that builds a Java 9 module?

    run {
    doFirst {
        jvmArgs = [
                '--module-path', classpath.asPath,
                '--patch-module', "$moduleName=" + files(sourceSets.main.output.resourcesDir).asPath,
                '--module', mainClassName
        ]       
    }
    }
    
David Miller
  • 694
  • 6
  • 7
0

So I have solved my issue by using java.io.File. Firstly I have created new File and checked where it is being saved. Then, since I have now known the root I have found my path to my resource and created a new File object - File myFile = new File("path_to_my_resource"). Then I got URL using toURI and toURL methods - myFile.toURI().toURL().

This is definitely hack but if someone is struggling like I was it can be used as a quick fix.

Druudik
  • 955
  • 1
  • 10
  • 32