0

I have problem with my JavaFX (Maven + H2 Database) project. It can be simple, but I've no idea of solution. Look at my project: here is my App class and pox.xml file, with run IDE shows Exception.

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

import java.io.IOException;

public class App extends Application {
    @Override
    public void start(Stage stage) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(App.class.getResource("src/main/resources/view/MainView"));
        Scene scene = new Scene(fxmlLoader.load(), 1280, 720);
        stage.setTitle("Finance Manager");
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }
}
<dependencies>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-controls</artifactId>
        <version>19.0.2.1</version>
    </dependency>
    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-fxml</artifactId>
        <version>19.0.2.1</version>
    </dependency>
</dependencies>
Exception in Application start method
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:119)
    at java.base/java.lang.reflect.Method.invoke(Method.java:578)
    at javafx.graphics@19.0.2.1/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:465)
    at javafx.graphics@19.0.2.1/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:364)
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
    at java.base/java.lang.reflect.Method.invoke(Method.java:578)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1081)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics@19.0.2.1/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:901)
    at javafx.graphics@19.0.2.1/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196)
    at java.base/java.lang.Thread.run(Thread.java:1623)
Caused by: java.lang.IllegalStateException: Location is not set.
    at javafx.fxml@19.0.2.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
    at javafx.fxml@19.0.2.1/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2523)
    at com.example.financemanager/pl.damianlebiedz.financemanager.App.start(App.java:14)
    at javafx.graphics@19.0.2.1/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847)
    at javafx.graphics@19.0.2.1/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
    at javafx.graphics@19.0.2.1/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:400)
    at javafx.graphics@19.0.2.1/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
    at javafx.graphics@19.0.2.1/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics@19.0.2.1/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics@19.0.2.1/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
    ... 1 more
Exception running application pl.damianlebiedz.financemanager.App

Process finished with exit code 1

I think that getResource fxml file path is correct, I don't know what is the problem.

Damian L
  • 29
  • 5
  • 1
    *"I think that getResource fxml file path is correct"*. No, the error message indicates that it is not. Resources are loaded at *run time* so they must be loaded from a location on the class path. The *source folder* `src` is typically not even available at run time. See https://stackoverflow.com/questions/61531317/how-do-i-determine-the-correct-path-for-fxml-files-css-files-images-and-other or the [Eden coding resource guide](https://edencoding.com/where-to-put-resource-files-in-javafx/) – James_D Aug 18 '23 at 14:47
  • 1
    The duplicate will help you understand the problem and solution, but in short, `src/main/resources` will **never** be part of a resource path in a typical Maven or Gradle setup. It's also typical for an FXML file to have an `.fxml` extension. So, try either `"/view/MainView"` or `"/view/MainView.fxml"`. – Slaw Aug 18 '23 at 14:53
  • 1
    As an aside, I recommend using either the latest LTS version of JavaFX (17.0.8) or the latest GA version (20.0.2). And unless paying for LTS services, I would go with the latest GA release. – Slaw Aug 18 '23 at 15:00
  • Thanks guys, the "/view/MainView.fxml" solution works well, but still I don't understand clearly why path made by me wasn't working - so I'm going to learn about it from James' pages, thanks once again! – Damian L Aug 18 '23 at 15:00

0 Answers0