1

I need help in determining the path to my FXML template.

My project:

com
    test
        abc
            xyz
                main
                    MainController.java
                    TEMPLATE.fxml
                Launcher.java
                

I need to access the TEMPLATE.fxml file in Launcher.java. I tried many different commands, but I always get null. What's the problem?

For some attempts, when I hold down the right CTRL, it finds the file from the IntelliJ level and opens it immediately. However, when compiling, I always get null.

Directory structure in IntelliJ IDEA

File: Launcher.java

package com.test.abc.xyz;

import com.test.abc.xyz.main.MainController;
import javafx.application.Application;
import javafx.stage.Stage;

public class Launcher extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {

        System.out.println("TEST1: " + this.getClass().getClassLoader().getResource("com/test/abc/xyz/main/TEMPLATE.fxml"));
        System.out.println("TEST2: " + this.getClass().getClassLoader().getResource("/com/test/abc/xyz/main/TEMPLATE.fxml"));
        System.out.println("TEST3: " + this.getClass().getResource("com/test/abc/xyz/main/TEMPLATE.fxml"));
        System.out.println("TEST4: " + this.getClass().getResource("/com/test/abc/xyz/main/TEMPLATE.fxml"));
        System.out.println("TEST5: " + getClass().getResource("com/test/abc/xyz/main/TEMPLATE.fxml"));
        System.out.println("TEST6: " + getClass().getResource("/com/test/abc/xyz/main/TEMPLATE.fxml"));

        System.out.println("TEST7: " + MainController.class.getClassLoader().getResource("main/TEMPLATE.fxml"));
        System.out.println("TEST8: " + MainController.class.getClassLoader().getResource("/main/TEMPLATE.fxml"));
        System.out.println("TEST9: " + MainController.class.getResource("main/TEMPLATE.fxml"));
        System.out.println("TEST10: " + MainController.class.getResource("/main/TEMPLATE.fxml"));

        System.out.println("TEST11: " + this.getClass().getClassLoader().getResource("src/main/java/com/test/abc/xyz/main/TEMPLATE.fxml"));
        System.out.println("TEST12: " + this.getClass().getClassLoader().getResource("/src/main/java/com/test/abc/xyz/main/TEMPLATE.fxml"));

        System.out.println("TEST13: " + this.getClass().getClassLoader().getResource("java/com/test/abc/xyz/main/TEMPLATE.fxml"));
        System.out.println("TEST14: " + this.getClass().getClassLoader().getResource("/java/com/test/abc/xyz/main/TEMPLATE.fxml"));

        System.out.println("TEST15: " + this.getClass().getResource("/com.test.abc.xyz.main/TEMPLATE.fxml"));
        System.out.println("TEST16: " + this.getClass().getResource("com.test.abc.xyz.main.TEMPLATE.fxml"));
        System.out.println("TEST17: " + this.getClass().getResource("com/test/abc/xyz/main/TEMPLATE.fxml"));

        System.out.println("TEST18: " + getClass().getClassLoader().getResource("main/TEMPLATE.fxml"));
        System.out.println("TEST19: " + getClass().getClassLoader().getResource("/main/TEMPLATE.fxml"));
    }
}

*** Output ***

TEST1: null
TEST2: null
TEST3: null
TEST4: null
TEST5: null
TEST6: null
TEST7: null
TEST8: null
TEST9: null
TEST10: null
TEST11: null
TEST12: null
TEST13: null
TEST14: null
TEST15: null
TEST16: null
TEST17: null
TEST18: null
TEST19: null

UPDATE 9.11.2020 19:16

Add to pom.xml solving a problem

<build>
    <resources>
        <resource>
            <directory>src/main/java</directory>
        </resource>
    </resources>
</build>

Source: https://maven.apache.org/plugins/maven-resources-plugin/examples/include-exclude.html

mAsHER
  • 33
  • 7
  • Looks like the FXML file is not being deployed to the build? See "Troubleshooting" in https://stackoverflow.com/questions/61531317/how-do-i-determine-the-correct-path-for-fxml-files-css-files-images-and-other What's in `target/classes/com/test/abc/xyz/main`? – James_D Nov 09 '20 at 17:37
  • @James_D Thanks for answer, I execute command: jar tf custom-1.0-SNAPSHOT.jar File TEMPLATE.fxml not included in result. Result: https://ibb.co/8dTS3fV – mAsHER Nov 09 '20 at 17:53
  • Then the configuration of your build tool (or IDE) is wrong. You should probably create `com/test/abc/xyz/main` under `src/main/resources` and put the FXML in there. Then clean and rebuild the project. – James_D Nov 09 '20 at 17:56
  • 1
    If you run it in the jar, do you package the resources in the jar? Is it present there? BTW, how do you build your jar? Your `fxml` is not in `resources`, so you're not using maven, gradle etc? – Dmitriy Popov Nov 09 '20 at 17:59
  • @James_D It is works! But I dont like the location :( do you know how to change in IntelliJ? – mAsHER Nov 09 '20 at 18:03
  • 1
    No; I don't use IntelliJ (much). You would need to figure out how to make `src/main/java` a *resource* folder, as well as a *source* folder. – James_D Nov 09 '20 at 18:04
  • Thank you! Solution: https://maven.apache.org/plugins/maven-resources-plugin/examples/include-exclude.html – mAsHER Nov 09 '20 at 18:14
  • You may want to consider getting used to separating the resources directory from the source directory. Both the default configurations of Maven and Gradle do this and it's a commonly accepted standard in the Java community. But of course you don't have to follow this standard in your own projects, especially if it's a personal project. – Slaw Nov 09 '20 at 23:37

0 Answers0