简体   繁体   中英

Maven JavaFX Project Unable to Find FXML FIle Using GetResource?

I have a standard (Non JavaFX) Maven Java Project in Netbeans. The first thing it is supposed to do is load a .fxml file, but I can't seem to find it using getClass().getResource() .

This is unusual, because this project is imported from a standard Java project, which runs just fine without error.

Here is the main method:

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

@Override
public void start(Stage stage) throws Exception {
    Main_PanelController controller = new Main_PanelController(this);
    FXMLLoader loader = new FXMLLoader(getClass().getResource("Main_Panel.fxml"));
    loader.setController(controller);
    URL url = getClass().getResource("Main_Panel.fxml");
    System.out.println("URL: " + url);
    System.exit(0);
    Parent root = loader.load();
    Scene scene = new Scene(root);
    stage.setScene(scene);
    stage.getIcons().add(new Image(new FileInputStream(Paths.get("Pictures", "SBU Emblem.png").toFile())));
    stage.setAlwaysOnTop(true);
    stage.setTitle("Simple Knisley Citation Tool α 0.5");
    stage.setResizable(false);
    stage.show();
}

The URL always comes back as null .

& The File Hierarchy:

等级制度

I have the System.out.println as a test method to see if the URL object comes back as null or not. According to everything I've read about getClass().getResource() it should get any object in the same package as the calling class, which Main_Panel.fxml is.

Here is the Jar file structure from the target folder after Clean & Build has run using the command jar tf Simple-Knisley-0.5-Alpha-jackofall.jar

META-INF/
META-INF/MANIFEST.MF
com/
com/protonmail/
com/protonmail/sarahszabo/
com/protonmail/sarahszabo/simpleknisley/
com/protonmail/sarahszabo/simpleknisley/core/
com/protonmail/sarahszabo/simpleknisley/core/CitationDiskManager.class
com/protonmail/sarahszabo/simpleknisley/core/Main_PanelController.class
com/protonmail/sarahszabo/simpleknisley/core/CitationGenerator$GeneratorType.class
com/protonmail/sarahszabo/simpleknisley/core/CitationGenerator.class
com/protonmail/sarahszabo/simpleknisley/core/Main.class
.netbeans_automatic_build
META-INF/maven/
META-INF/maven/com.protonmail.sarahszabo.simpleknisley/
META-INF/maven/com.protonmail.sarahszabo.simpleknisley/Simple-Knisley/
META-INF/maven/com.protonmail.sarahszabo.simpleknisley/Simple-Knisley/pom.xml
META-INF/maven/com.protonmail.sarahszabo.simpleknisley/Simple-Knisley/pom.properties

I'm using Java 8_171 on Netbeans 8.2 on Kubuntu 18.04 Bionic Beaver

There are a few issues going on here.

  1. Maven expects (by default) resources to be in the src/main/resources directory. It will only copy java files from src/main/java by default. So move your fxml file to src/main/resources/com/protonmail/sarahszabo/simpleknisley/core/Main_Panel.fxml

  2. getClass().getResource() expects you to give the full path to the resource that you want to load. So in your case: getClass().getResource("/com/protonmail/sarahszabo/simpleknisley/core/Main_Panel.fxml")

Using getClass().getClassLoader().getResource() allows you to specify the location of the resource you want to load as a relative path to the class you are calling it in:

getClass().getClassLoader().getResource("Main_Panel.fxml")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM