0

I am trying to run a simple JavaFX file. See my code below.

package com.example.multiplayer_snake;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;

public class HelloApplication extends Application {
    @SuppressWarnings("exports")
    @Override
    public void start(Stage stage) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("/Multiplayer_Snake/src/main/resources/com/example/multiplayer_snake/hello-view.fxml"));
        Scene scene = new Scene(fxmlLoader.load(), 600, 400);
        stage.setTitle("Snake");
        stage.setScene(scene);
        stage.show();
    }

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

And my folder structure is looking like in this picture

enter image description here

What I've figured out so far is it has something to do with my absolute path to the .fxml file. If I manually copy the .fxml file into my package and change the path to "hello-view.fxml" I am able to run it. But I don't always want to move/copy the file manually whenever the .fxml file is changed.

"/Multiplayer_Snake/src/main/resources/com/example/multiplayer_snake/hello-view.fxml"

But I don't really get why I am still running into errors. Any solutions how to determine the correct path?

Daniel
  • 51
  • 1
  • 7
  • @kleopatra In Maven, it should be configured as a *resource* folder, not a *source* folder. – James_D Oct 27 '21 at 13:39
  • Check the contents of `target/classes`. If your FXML file is not in the expected package, something is wrong with your build configuration. – James_D Oct 27 '21 at 13:40
  • @James_D yeah, but don't see any mention of maven in the question, maybe being blind :) – kleopatra Oct 27 '21 at 13:41
  • @kleopatra There's a `pom.xml` in the screenshot... So, reasonable assumption? (Mind you, there's an `.iml` file too, which doesn't make much sense in an Eclipse project.) – James_D Oct 27 '21 at 13:42
  • 1
    Does this help? https://stackoverflow.com/questions/19602727/how-to-reference-javafx-fxml-files-in-resource-folder – Abra Oct 27 '21 at 13:43
  • @James_D ahhh ... thanks for the eye opener :) – kleopatra Oct 27 '21 at 15:39

0 Answers0