Почему возникает ошибка javafx.fxml.LoadException

Dmitry Lin

package LinLab_System;

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

    import java.net.URL;

     public class Main extends Application {
      private static Scene initialWindow;
      static Scene settings;
      public static Stage prStage;

     @Override
     public void start(Stage primaryStage) throws Exception{
         prStage=primaryStage;
         URL ur = getClass().getResource("Initial_window.fxml");
         initialWindow=new Scene(FXMLLoader.load(ur), 900, 600);
         settings=new Scene(FXMLLoader.load(getClass().getResource("settings/settings.fxml")), 280, 270);
         primaryStage.setTitle("LinLab System (LLS)");
         primaryStage.setScene(initialWindow);
         primaryStage.getIcons().add(new Image("file:src/LinLab_System/images/Logo.jpg"));
         primaryStage.setResizable(false);
         primaryStage.show();
     }


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

Создал артефакт из этого проекта и пытался запустить полученный джарник В IntellijIdea 16 всё работает, но когда пытаюсь запустить .jar выдаёт ошибку.

Это отчёт об ошибке

Executing C:\Users\Dmitrij\Documents\NetBeansProjects\JavaFXProject1\dist\run246249056\JavaFXProject1.jar using platform C:\Program Files\Java\jdk1.8.0_121\jre/bin/java
Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:745)
Caused by: javafx.fxml.LoadException: 
file:/C:/Users/Dmitrij/Documents/NetBeansProjects/JavaFXProject1/dist/run246249056/JavaFXProject1.jar!/LinLab_System/Initial_window.fxml

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at LinLab_System.Main.start(Main.java:20)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    ... 1 more
Caused by: java.lang.IllegalArgumentException: URI is not hierarchical
    at java.io.File.<init>(File.java:418)
    at LinLab_System.Initial_Window_Controller.initialize(Initial_Window_Controller.java:46)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
    ... 17 more
</init>

Как именно правильно указать ссылку на файл, находящийся в проекте?

1 ответ

Dmitry Lin

Получите ресурс как поток: InputStream stream = getClass().getResourceAsStream("Initial_window.fxml"); и передайте его потом в метод new FXMLLoader().load(stream)

licensed under cc by-sa 3.0 with attribution.