2 Replies Latest reply on May 18, 2012 7:50 AM by 935832

    Problem with running the app outside NetBeans (loading fxml from jar)

      this problem occurs only when NOT using NetBeans (becouse NB may use this normal file sctructure, without jars)
      here is part of my main class, start method
              //load FXMLs
              FXMLLoader primaryLoader = new FXMLLoader();
              URL primaryLocation = getClass().getResource("PrimaryStage.fxml");
              Parent root = (Parent) primaryLoader.load();
              Scene primaryScene = new Scene(root);
              FXMLLoader secondaryLoader = new FXMLLoader();
              URL secondaryLocation = getClass().getResource("SecondaryStage.fxml");
              System.out.println("sec loader = " +secondaryLoader.toString()
                      +"\n sec loc from loader = "+secondaryLoader.getLocation()
                      +"\n sec loc url = " + secondaryLocation
                      +"\n pri loc url = " + primaryLocation);
              Parent secondaryRoot = (Parent) secondaryLoader.load();
              Scene secondaryScene = new Scene(secondaryRoot);
      and the thing is the 1st url (primaryLocation) is loaded properly, and the secondLocation aint.
      those 2 files are both in the same jar, same folder (packege) as main class.

      the outcome looks like this
      sec loader = javafx.fxml.FXMLLoader@5bde2a20
       sec loc from loader = null
       sec loc url = null
       pri loc url = jar:file:/C:/test_workspace/NetBeans/MZlike/dist/MoreAdvStuff.jar!/moreadvstuff/PrimaryStage.fxml
      Exception in Application start method
              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.javafx.main.Main.launchApp(Main.java:486)
              at com.javafx.main.Main.main(Main.java:638)
      Caused by: java.lang.RuntimeException: Exception in Application start method
              at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
              at com.sun.javafx.application.LauncherImpl.access$000(Unknown Source)
              at com.sun.javafx.application.LauncherImpl$1.run(Unknown Source)
              at java.lang.Thread.run(Unknown Source)
      Caused by: java.lang.IllegalStateException: Location is not set.
              at javafx.fxml.FXMLLoader.load(Unknown Source)
              at moreadvstuff.MoreAdvStuff.start(MoreAdvStuff.java:55)
              at com.sun.javafx.application.LauncherImpl$5.run(Unknown Source)
              at com.sun.javafx.application.PlatformImpl$4.run(Unknown Source)
              at com.sun.javafx.application.PlatformImpl$3.run(Unknown Source)
              at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
              at com.sun.glass.ui.win.WinApplication.access$100(Unknown Source)
              at com.sun.glass.ui.win.WinApplication$2$1.run(Unknown Source)
              ... 1 more
      where 55 line is
      Parent secondaryRoot = (Parent) secondaryLoader.load();
      any ideas how to fix it? loading the file manually from the jar may be a way, but its a workaround, not a solution

      Edited by: Radosław on 2012-05-17 07:55
        • 1. Re: Problem with running the app outside NetBeans (loading fxml from jar)

          The root cause of the exception appears is here:
           sec loc url = null
          This means that the URL returned here by getResource() was null:
                  URL secondaryLocation = getClass().getResource("SecondaryStage.fxml");
          So the only thing you have to do is figure out why this call returned null. Usually it returns null if the resource is not found in the jar/classpath.

          Last time it happened to me it was because I had misspelled the file name.
          Check that your file is not named SecondaryStage,fxml instead of SecondaryStage.fxml or something like that. Also check that it's actually present in the jar - or in the build/ directory and at the expected place (next to the class that loads it).

          Hope this helps,

          -- daniel
          • 2. Re: Problem with running the app outside NetBeans (loading fxml from jar)
            Hello Daniel.
            u were right. i am real dummy.
            i just thought that if netbeans gets it - every name is all right
            after double checking all names i found typo.

            thx for forcing me to double check this

            best regards