0

I am trying to load my *.fxml files, which are located in a differnt package, packed in a JAR.

My folder structure: (all folders are in the classpath)

- src/main/java: 
    - com.test.controller
        - Application.java
    - com.test.view
        - Main.fxml
- src/main/test: contains unit tests
- src/main/resources: contains images

As mentioned here I use the following statement to load the FXML file. (without the leading slash "/") The method is called in the Application.java file.

FXMLLoader loader = new FXMLLoader(getClass().getClassLoader().getResource("com/test/Main.fxml")

This works quite fine if I run the project within eclipse, but fails with a java.lang.IllegalStateException: Location is not set if I execute the JAR (which was created with Gradle)

If I unpack the JAR file, I can see the Main.fxml under /com/test/, so the file does exist.

I tried several variations of accessing the FXML file in a different package, but nothing worked. Does somebody know what to do here?

I would rather not move the FXML File to the resource folder (like stated here, because I assume it should be either possible to access the files where they are now.

1 Answers1

0

Add this to your build.gradle (if you use Gradle as build tool)

sourceSets {
    main {
        resources {
            srcDirs = ["src/main/java", "src/main/resources"]
        }
    }
}

And then load the resources as following:

Option 1: Relative to the current class file (MyClass):

MyClass.class.getResource("../view/Main.fxml")

Option 2: Relative to the classpath root

URL configUrl = MyClass.class.getClassLoader().getResource("config.xml");
File myConfigFile = new File(configUrl.toURI());`