0

I have problems loading my fxml files when starting my Project from jar (jar was generated with maven). Igore the System.outs theyre only for quick Debugging purposes.

I use the following code (this code is called from the class TabPaneTest):

FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("TabPaneView.fxml"));

This is the initialize Method of the corresponding Controller:

@FXML
public void initialize() {
    this.tabPane1.setTabClosingPolicy(TabClosingPolicy.ALL_TABS);
    this.loginTab.setClosable(false);
    this.loginTab.setText("Login");
    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("../view/ConnectView.fxml"));
    //Funktionierende jar
    //FXMLLoader fxmlLoader = new FXMLLoader(new URL("rsrc:resources/de/statistik_nord/klr/view/EingabeView.fxml"));
    //System.out.println(getClass().getClassLoader().getResourceAsStream("resources/de/statistik_nord/klr/view/EingabeView.fxml"));

    fxmlLoader.setController(new ConnectController());
    Parent root = null;
    //Parent root = FXMLLoader.load(getClass().getResource("view/ConnectView.fxml"));
    try {
        root = (Parent) fxmlLoader.load(); //This is TabPaneController.java:52, which is mentioned in the stacktrace
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    this.loginTab.setContent(root);
    dbc.bind(((ConnectController) fxmlLoader.getController()).getDbcProperty());
    this.dbc.addListener(new ChangeListener<DBConnection>() {
        @Override
        public void changed(ObservableValue<? extends DBConnection> observable, DBConnection oldValue, DBConnection newValue) {
            // TODO Auto-generated method stub
         {
            if(newValue != null) {
                System.out.println(dbc + "new");
                ec = new EingabeController(dbc.getValue());
                changeExistingTab(loginTab, "Eingabe", "../view/EingabeView.fxml", ec, (ActionEvent e) -> {

                    {
                        if (e.getTarget() instanceof Button) {
                            switch(((Button) e.getTarget()).getId()) {
                            case "buttonUebersKTR":
                                openNewTab(new Tab(), "Uebersicht Kostentraeger", "../view/Übersicht_KTRView.fxml", new Uebersicht_KTRController(dbc.getValue(), ec.getChosenPersonal()));
                                break;
                            case "buttonPersonalKTR":
                                openNewTab(new Tab(), "Personalübersicht für: ", "../view/Personal_KTR_UebersView.fxml", new Personal_KTR_UebersController(dbc.getValue(), ec.getChosenKTR()));
                                break;
                            case "buttonUnvollstaendig":
                                openNewTab(new Tab(), "Unvollstaendig", "../view/UnvollstaendigView.fxml", new UnvollstaendigController(dbc.getValue()));
                                break;
                            case "buttonLogOut":
                                dbc.removeListener(this);
                                tabPane1.getTabs().removeIf(tab -> !(tab.getText().equals("Eingabe")));
                                initialize();
                                System.out.println(dbc + "case");
                                break;
                            }
                        } else {
                            System.out.println("Event!!!!");
                        }

                    }

                });
            }
        }



        }

    });

}

And the Directorys inside the jar look like this:

jar directories jar directories 2

I get the following error:

Caused by javafx.fxml.LoadException: File:/....../[...].jar!/de/statistik_nord/klr/TabPaneView.fxml

I also print the path and get this (when exetung the jar):

System.out.println(getClass().getResource("TabPaneView.fxml"));
jar:file:/...../[...].jar!/de/statistik_nord_klr/TabPaneView.fxml

Starting the program from eclipse works just fine and has basically the same packaging:

eclipse directories

edit:

Stacktrace:

/* This is sys.out.print output I do myself 
TEEEEEEEEEEEEST
jar:file:/C:/Users/WollscPh/eclipse- workspace/klr_fx_test/target/klr_fx_test-0.0.1-SNAPSHOT.jar!/de/statistik_nord/klr/view/TabPaneView.fxml
TEEEEEEEEEEEEST
*/
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
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(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
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$154(LauncherImpl.java:182)
at java.lang.Thread.run(Unknown Source)
Caused by: javafx.fxml.LoadException: 
file:/C:/Users/WollscPh/eclipse-workspace/klr_fx_test/target/klr_fx_test-0.0.1-SNAPSHOT.jar!/de/statistik_nord/klr/TabPaneView.fxml

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2571)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at de.statistik_nord.klr.TabPaneTest.start(TabPaneTest.java:37)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(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$147(WinApplication.java:177)
... 1 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.Trampoline.invoke(Unknown Source)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2566)
... 12 more
Caused by: java.lang.IllegalStateException: Location is not set.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2434)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at de.statistik_nord.klr.controller.TabPaneController.initialize(TabPaneController.java:52)
... 22 more
Exception running application de.statistik_nord.klr.TabPaneTest

the fxml file (TabPaneView, loaded in TabPaneTest.java, the main class):

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.layout.BorderPane?>

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1">
<center>
</center>
<center>
  <TabPane fx:id="tabPane1" prefHeight="200.0" prefWidth="200.0" tabClosingPolicy="UNAVAILABLE" BorderPane.alignment="CENTER">
    <tabs>
      <Tab fx:id="loginTab" text="Untitled Tab 1" />
    </tabs>
  </TabPane>
</center>
</BorderPane>

The fxml file (ConnectView, which is the second loaded fxml and loaded from the initialize method of TabPaneController)

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.BorderPane?>

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1">
<left>
  <TextField id="text1" fx:id="textField1" prefHeight="48.0" prefWidth="149.0" promptText="User" BorderPane.alignment="CENTER" />
</left>
<right>
  <TextField id="text2" fx:id="textField2" prefHeight="48.0" prefWidth="149.0" promptText="Passwort" BorderPane.alignment="CENTER" />
</right>
<center>
  <Button fx:id="button1" mnemonicParsing="false" text="LogIn" BorderPane.alignment="CENTER" />
</center>
</BorderPane>

Just in case Ill include the corresponding Controller for the ConnectView too, although theres not really anything Happening:

@FXML
public void initialize() {
    // TODO Auto-generated method stub
    button1.setOnAction(new EventHandler<ActionEvent>() {
        public void handle(ActionEvent event) {

            dbc.set(new DBConnection(textField1.getText(), textField2.getText()));
        }
    });
}
goldarm5
  • 49
  • 8
  • Could you provide the complete stacktrace and possibly relevant parts of the fxml. It looks like this is not an issue with finding the resource, but an error that happens while processing the fxml. – fabian Nov 15 '18 at 15:56
  • Will do that, but the stacktrace is in an only restricted resizable Windows cmd so it might look a bit messy. About processing the fxml, shouldnt that occur in eclipse too then? Because I can load it from eclipse I only get a warning: "WARNING: Loading FXML document with JavaFX API of version 8.0.171 by JavaFX runtime of version 8.0.111" or might this be the issue? – goldarm5 Nov 15 '18 at 16:00
  • You could write the output to a text file, see https://stackoverflow.com/questions/1420965/redirect-windows-cmd-stdout-and-stderr-to-a-single-file . BTW: did you check, if the code you posted is the source of the issue. The stacktrace indicates that `TabPaneController`'s `initialize` method is used to load a fxml, so maybe the source of the issue was another load call. – fabian Nov 15 '18 at 16:22
  • The TabPaneController is used to load all the tabs and loads the “start“ tab in its initialize method, which is also working perfectly fine in eclipse. I will take a look at it and edit tommorow. – goldarm5 Nov 15 '18 at 16:53
  • I included the second fxm, both controlers initialize methods and the stacktrace in a codeblock. As you pointed out there is an error with loading the second fxml. The TabPaneController which tries to load the ConnectView.fxml is in the Controller directory/package (look at jar directories imgur link) and the ConnectView.fxml isin the view Directory/package. @fabian – goldarm5 Nov 18 '18 at 22:50

1 Answers1

0

The mistake was using .. in the relative path I used:

FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("../view/ConnectView.fxml"));

This exists in the normal filesystem, but not in the jar, which explains why it worked if I started it from eclipse but not when I started it from the executable jar. The solution to this is the following:

FXMLLoader fxmlLoader = new FXMLLoader(getClass().getClassLoader().getResource("de/statistik_nord/klr/view/ConnectView.fxml"));

For reference the path to ConnectView.fxml is this: MyJarName.jar/de/statistik_nord/klar/view/ConnectView.fxml.

goldarm5
  • 49
  • 8