Loading file from resources when using modules

Viewed 780

I am trying to load a FXML file from resources directory to show a JavaFX window, however whatever I do I am unable to find the correct way to reference the resource.

My directory structure is as follows:

src
    main
        java
            gui
                App.java
            module-info.java
        resources
            MainWindow.fxml
out
    production
        classes
            // All compiled classes
        resources
            MainWindow.fxml

The contents of module-info.java are following:

module MyApp {
    requires javafx.controls;
    requires javafx.graphics;
    requires javafx.fxml;
    exports gui;
}

I'm trying to load the MainWindow.fxml file in App.java like this: getClass().getResource("/MainWindow.fxml");

I've tried using MainWindow.fxml and /MainWindow.fxml names, however both return null. As can be seen from the directory structure, contents of resources are copied as well, so they should be available at runtime. Resources directory is marked as such in IntelliJ. I'm using Gradle for dependency management.

How can I load files from the resources directory?

Here's my build.gradle file:

plugins {
    id 'application'
    id 'java'
    id 'org.openjfx.javafxplugin' version '0.0.5'
}

group 'MyApp'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.11

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

repositories {
    mavenCentral()
}

dependencies {
    testImplementation group: 'junit', name: 'junit', version: '4.12'

    implementation 'org.openjfx.javafx-controls:11.0.1'
    implementation 'org.openjfx.javafx-fxml:11.0.1'
    implementation 'org.openjfx.javafx-graphics:11.0.1'
}
0 Answers
Related