Loading file from resources when using modules












4















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'
}









share|improve this question

























  • Maybe: App.class.getResource("../resources/MainWindow.fxml");?

    – Flown
    Jan 2 at 19:40











  • I've tried "../resources/MainWindow.fxml", "/resources/MainWindow.fxml", "resources/MainWindow.fxml", neither of them return a resource.

    – tofiffe
    Jan 2 at 19:45











  • For starters, you should open your project to the fxml module, like opens gui to javafx.fxml, as in this sample. Are you using the JavaFX gradle plugin? Can you show your build.gradle file?

    – José Pereda
    Jan 2 at 20:19











  • @JoséPereda I've added the build.gradle file. I've also added the opens statement, but I think this may be some other issue, since the file cannot be loaded before any related JavaFX is executed.

    – tofiffe
    Jan 2 at 20:25











  • You don't need the implementation part, the plugin does that for you. Also you don't need to add javafx.graphics, it is added by controls. As for your issue, do you have an exception? Can you post the stacktrace? Have you tried to run the sample I linked before, does it work for you?

    – José Pereda
    Jan 2 at 20:28
















4















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'
}









share|improve this question

























  • Maybe: App.class.getResource("../resources/MainWindow.fxml");?

    – Flown
    Jan 2 at 19:40











  • I've tried "../resources/MainWindow.fxml", "/resources/MainWindow.fxml", "resources/MainWindow.fxml", neither of them return a resource.

    – tofiffe
    Jan 2 at 19:45











  • For starters, you should open your project to the fxml module, like opens gui to javafx.fxml, as in this sample. Are you using the JavaFX gradle plugin? Can you show your build.gradle file?

    – José Pereda
    Jan 2 at 20:19











  • @JoséPereda I've added the build.gradle file. I've also added the opens statement, but I think this may be some other issue, since the file cannot be loaded before any related JavaFX is executed.

    – tofiffe
    Jan 2 at 20:25











  • You don't need the implementation part, the plugin does that for you. Also you don't need to add javafx.graphics, it is added by controls. As for your issue, do you have an exception? Can you post the stacktrace? Have you tried to run the sample I linked before, does it work for you?

    – José Pereda
    Jan 2 at 20:28














4












4








4


1






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'
}









share|improve this question
















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'
}






java intellij-idea javafx java-11






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 4 at 20:43









Mikhail Kholodkov

5,15652951




5,15652951










asked Jan 2 at 19:32









tofiffetofiffe

1,45611030




1,45611030













  • Maybe: App.class.getResource("../resources/MainWindow.fxml");?

    – Flown
    Jan 2 at 19:40











  • I've tried "../resources/MainWindow.fxml", "/resources/MainWindow.fxml", "resources/MainWindow.fxml", neither of them return a resource.

    – tofiffe
    Jan 2 at 19:45











  • For starters, you should open your project to the fxml module, like opens gui to javafx.fxml, as in this sample. Are you using the JavaFX gradle plugin? Can you show your build.gradle file?

    – José Pereda
    Jan 2 at 20:19











  • @JoséPereda I've added the build.gradle file. I've also added the opens statement, but I think this may be some other issue, since the file cannot be loaded before any related JavaFX is executed.

    – tofiffe
    Jan 2 at 20:25











  • You don't need the implementation part, the plugin does that for you. Also you don't need to add javafx.graphics, it is added by controls. As for your issue, do you have an exception? Can you post the stacktrace? Have you tried to run the sample I linked before, does it work for you?

    – José Pereda
    Jan 2 at 20:28



















  • Maybe: App.class.getResource("../resources/MainWindow.fxml");?

    – Flown
    Jan 2 at 19:40











  • I've tried "../resources/MainWindow.fxml", "/resources/MainWindow.fxml", "resources/MainWindow.fxml", neither of them return a resource.

    – tofiffe
    Jan 2 at 19:45











  • For starters, you should open your project to the fxml module, like opens gui to javafx.fxml, as in this sample. Are you using the JavaFX gradle plugin? Can you show your build.gradle file?

    – José Pereda
    Jan 2 at 20:19











  • @JoséPereda I've added the build.gradle file. I've also added the opens statement, but I think this may be some other issue, since the file cannot be loaded before any related JavaFX is executed.

    – tofiffe
    Jan 2 at 20:25











  • You don't need the implementation part, the plugin does that for you. Also you don't need to add javafx.graphics, it is added by controls. As for your issue, do you have an exception? Can you post the stacktrace? Have you tried to run the sample I linked before, does it work for you?

    – José Pereda
    Jan 2 at 20:28

















Maybe: App.class.getResource("../resources/MainWindow.fxml");?

– Flown
Jan 2 at 19:40





Maybe: App.class.getResource("../resources/MainWindow.fxml");?

– Flown
Jan 2 at 19:40













I've tried "../resources/MainWindow.fxml", "/resources/MainWindow.fxml", "resources/MainWindow.fxml", neither of them return a resource.

– tofiffe
Jan 2 at 19:45





I've tried "../resources/MainWindow.fxml", "/resources/MainWindow.fxml", "resources/MainWindow.fxml", neither of them return a resource.

– tofiffe
Jan 2 at 19:45













For starters, you should open your project to the fxml module, like opens gui to javafx.fxml, as in this sample. Are you using the JavaFX gradle plugin? Can you show your build.gradle file?

– José Pereda
Jan 2 at 20:19





For starters, you should open your project to the fxml module, like opens gui to javafx.fxml, as in this sample. Are you using the JavaFX gradle plugin? Can you show your build.gradle file?

– José Pereda
Jan 2 at 20:19













@JoséPereda I've added the build.gradle file. I've also added the opens statement, but I think this may be some other issue, since the file cannot be loaded before any related JavaFX is executed.

– tofiffe
Jan 2 at 20:25





@JoséPereda I've added the build.gradle file. I've also added the opens statement, but I think this may be some other issue, since the file cannot be loaded before any related JavaFX is executed.

– tofiffe
Jan 2 at 20:25













You don't need the implementation part, the plugin does that for you. Also you don't need to add javafx.graphics, it is added by controls. As for your issue, do you have an exception? Can you post the stacktrace? Have you tried to run the sample I linked before, does it work for you?

– José Pereda
Jan 2 at 20:28





You don't need the implementation part, the plugin does that for you. Also you don't need to add javafx.graphics, it is added by controls. As for your issue, do you have an exception? Can you post the stacktrace? Have you tried to run the sample I linked before, does it work for you?

– José Pereda
Jan 2 at 20:28












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54012148%2floading-file-from-resources-when-using-modules%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54012148%2floading-file-from-resources-when-using-modules%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

SQL update select statement

WPF add header to Image with URL pettitions [duplicate]