Hải Võ
Hải Võ

Reputation: 1

Problem of getting resource using getResource()

I need to get a resource FXML file in a Java project. Here's my code:

public void start(Stage stage) throws IOException {
    try {
//        System.out.println(getClass());
        URL url = this.getClass().getResource("/org/example/ooplibrary/View/LogInView.fxml");
        if (url == null) {
            throw new IOException("FXML file not found");
        }
        Parent root = FXMLLoader.load(url);
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}

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

The directory structure is the following

org/
   example/
        ooplibrary/
                 Core/
                    Main.java
                 View/
                    LogInView.fxml

The getClass() works normally but when I add the getResource(), the output was always null. I have tried all the paths but it didn't work. Can anyone give me a hint?

Upvotes: 0

Views: 63

Answers (1)

Wladimir Diskowski
Wladimir Diskowski

Reputation: 650

Place your LogInView.fxml File not in

src/main/java/org/example/ooplibrary/View/LogInView.fxml

but in

src/main/resources/org/example/ooplibrary/View/LogInView.fxml

Upvotes: 1

Related Questions