Gradle not including FXML and images

build, continuous-integration, gradle, intellij-idea, java

Solution

You are putting xml files in a folder called `src/main/java` (hint: java dir is for java files)

Resources (eg xml, image files etc) should go in `src/main/resources`

This is a standard convention followed by both Maven and Gradle. If you want to change the defaults you can, but first I strongly advise you consider following the sensible conventions.

Also, a note on classloaders

- SomeClass.class.getResource("foo.xml") will look for the xml relative to the class package

- SomeClass.class.getResource("/foo.xml") will look for the xml relative to the classpath root

So in your example

getClass().getResourceAsStream("layoutForm.fxml")

This is looking for a file on the classpath `com/voidustries/poly/gui/layoutForm.fxml`

So if you want to leave your resources where they are, you will want to add `src/main/java` to the resource folders, not `src/main/java/com/voidustries/poly`

Problem

I have been looking around for the proper way to include FXML and images in the `build.gradle` so that they are build into the jar. I have look here and tried it but it still does not work. Throws this error: `java.lang.NullPointerException: inputStream is null.` Here is the `build.gradle` (Github): ``` group 'com.voidustries' version '1.0-SNAPSHOT' sourceSets { main { resources { srcDirs = ["src/main/java/com/voidustries/poly"] includes = ["gui/layoutForm.fxml"] } } } buildscript { repositories { mavenCentral() } dependencies { classpath 'org.junit.platform:junit-platform-gradle-plugin:1.1.0' } } apply plugin: 'java' apply plugin: 'org.junit.platform.gradle.plugin' repositories { mavenCentral() } dependencies { testCompile('org.junit.jupiter:junit-jupiter-api:5.1.0') testRuntime('org.junit.jupiter:junit-jupiter-engine:5.1.0') } task wrapper(type: Wrapper) { description = 'Generates gradlew[.bat] scripts' gradleVersion = '4.3.1' } test { testLogging { events "passed", "skipped", "failed" } } ``` Here is GUI.java (Github): ``` FXMLLoader loader = new FXMLLoader(); FileInputStream fis = new FileInputStream(settings); ResourceBundle resourceBundle = new PropertyResourceBundle(fis); loader.setResources(resourceBundle); Parent root = loader.load(getClass().getResourceAsStream("layoutForm.fxml")); Platform.setImplicitExit(false); ``` The stacktrace says that `loader.load(getClass().getResourceAsStream("layoutForm.fxml"));` is causing the error Here is the source Tree: ``` com └───voidustries └───poly │ CustomFormatter.java │ Main.java │ PolyThreads.java │ SysTray.java │ ├───gui │ Controller.java │ GUI.java │ Icon.png │ layoutForm.fxml │ └───img Icon.png ``` you can see that `layoutForm.fxml` and `Icon.png` are both there in the tree Here is the Out tree: ``` com └───voidustries └───poly │ CustomFormatter.class │ Main.class │ PolyThreads.class │ SysTray.class │ └───gui Controller.class GUI.class ``` In the out tree they are both absent and I have been fiddling for hours trying to fix this issue. Any help would be greatly appreciated. Also if you need more here is the Github repository

Original source

Related problems