openjfx / samples

JavaFX samples to run with different options and build tools
BSD 3-Clause "New" or "Revised" License
578 stars 1.52k forks source link

IntelliJ + modular + Gradle does not load resources when running from IntelliJ #34

Open gerritsangel opened 4 years ago

gerritsangel commented 4 years ago

Hello,

The example for Intellij + modular + Gradle does not start when invoked directly from IntelliJ. It does not find the fxml file here:

    Parent root = FXMLLoader.load(getClass().getResource("scene.fxml"));

and throws a NPE.

Is there a way to fix this with the Gradle setup? Starting the application with gradlew run works, but then I don't have a debugger support and it is not straightforward. I think the problem is that the resource files are build to a different output directory (build/classes and build/resources), but IntelliJ does not merge the directories.

I've imported the project into IntelliJ on Windows 7, set JDK 12 as the Gradle and the Project SDK, but did not do any other changes.

jperedadnr commented 4 years ago

This is an issue with IntelliJ "default" configuration and how your project is configured as a "gradle" project.

Even if you don't set any VM arguments, IntelliJ will apply them for you. If you have a modular app, it puts everything in the module-path, and that's ok, but misses to add a required --patch-module: your resources and your Java classes are under the same package name but in different folders. That will leads to a "split-package" problem.

Therefore, the JavaFX gradle plugin adds this patch, and solves the issue, but this works only if you run a Gradle task (./gradlew run).

If you run from IntelliJ you will either have to:

alexspurling commented 3 years ago

@jperedadnr Could you provide some more detailed instructions on how to get the sample applications to work in IntelliJ? You say "edit the run configuration" - which part of the configuration exactly? Also, what should I replace "hellofx" in your example with? What does this represent exactly? Also, what should I place in "/path/to/resources" as this is obviously not a real path. Again, what does this represent exactly? Finally, could you please point to any kind of documentation either specific to JavaFX or to Java in general where this might already be explained in more detail.