Include FXML throws Exception from .jar












0














I am trying to include an fxml file in another one and deploy my application as a runnable jar.



The top-level fxml file is loaded like this:



URL location = Editor.class.getClassLoader().getResource("view/app.fxml");
FXMLLoader fxmlLoader = new FXMLLoader(location);
Parent root = fxmlLoader.load();
appController = fxmlLoader.getController();


app.fxml, which is located in "src/view" contains this line:



<fx:include fx:id="console" source="console.fxml" />


Running this in eclipse will work as expected, but running the exported .jar file will print:



Exception in Application start method
Exception in thread "main" java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:61)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: javafx.fxml.LoadException:
view/app.fxml:92

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2625)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2603)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2435)
at de.hsa.dice.editor.Editor.start(Editor.java:49)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
... 1 more
Caused by: java.net.MalformedURLException: Could not open InputStream for URL 'rsrc:console.fxml'
at org.eclipse.jdt.internal.jarinjarloader.RsrcURLConnection.getInputStream(RsrcURLConnection.java:49)
at java.base/java.net.URL.openStream(URL.java:1117)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2465)
at javafx.fxml.FXMLLoader.access$2700(FXMLLoader.java:105)
at javafx.fxml.FXMLLoader$IncludeElement.constructValue(FXMLLoader.java:1154)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:754)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2722)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2552)
... 12 more


Note that the console.fxml file is in the same package as app.fxml (in both the IDE and the .jar file).
That's why I also tried source="./console.fxml", but nothing changed.










share|improve this question






















  • nobody, really? I thought this might be a common scenario... because how else would anyone use the include tag in a production environment?
    – EzPizza
    Nov 23 '18 at 23:41
















0














I am trying to include an fxml file in another one and deploy my application as a runnable jar.



The top-level fxml file is loaded like this:



URL location = Editor.class.getClassLoader().getResource("view/app.fxml");
FXMLLoader fxmlLoader = new FXMLLoader(location);
Parent root = fxmlLoader.load();
appController = fxmlLoader.getController();


app.fxml, which is located in "src/view" contains this line:



<fx:include fx:id="console" source="console.fxml" />


Running this in eclipse will work as expected, but running the exported .jar file will print:



Exception in Application start method
Exception in thread "main" java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:61)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: javafx.fxml.LoadException:
view/app.fxml:92

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2625)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2603)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2435)
at de.hsa.dice.editor.Editor.start(Editor.java:49)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
... 1 more
Caused by: java.net.MalformedURLException: Could not open InputStream for URL 'rsrc:console.fxml'
at org.eclipse.jdt.internal.jarinjarloader.RsrcURLConnection.getInputStream(RsrcURLConnection.java:49)
at java.base/java.net.URL.openStream(URL.java:1117)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2465)
at javafx.fxml.FXMLLoader.access$2700(FXMLLoader.java:105)
at javafx.fxml.FXMLLoader$IncludeElement.constructValue(FXMLLoader.java:1154)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:754)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2722)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2552)
... 12 more


Note that the console.fxml file is in the same package as app.fxml (in both the IDE and the .jar file).
That's why I also tried source="./console.fxml", but nothing changed.










share|improve this question






















  • nobody, really? I thought this might be a common scenario... because how else would anyone use the include tag in a production environment?
    – EzPizza
    Nov 23 '18 at 23:41














0












0








0


0





I am trying to include an fxml file in another one and deploy my application as a runnable jar.



The top-level fxml file is loaded like this:



URL location = Editor.class.getClassLoader().getResource("view/app.fxml");
FXMLLoader fxmlLoader = new FXMLLoader(location);
Parent root = fxmlLoader.load();
appController = fxmlLoader.getController();


app.fxml, which is located in "src/view" contains this line:



<fx:include fx:id="console" source="console.fxml" />


Running this in eclipse will work as expected, but running the exported .jar file will print:



Exception in Application start method
Exception in thread "main" java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:61)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: javafx.fxml.LoadException:
view/app.fxml:92

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2625)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2603)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2435)
at de.hsa.dice.editor.Editor.start(Editor.java:49)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
... 1 more
Caused by: java.net.MalformedURLException: Could not open InputStream for URL 'rsrc:console.fxml'
at org.eclipse.jdt.internal.jarinjarloader.RsrcURLConnection.getInputStream(RsrcURLConnection.java:49)
at java.base/java.net.URL.openStream(URL.java:1117)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2465)
at javafx.fxml.FXMLLoader.access$2700(FXMLLoader.java:105)
at javafx.fxml.FXMLLoader$IncludeElement.constructValue(FXMLLoader.java:1154)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:754)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2722)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2552)
... 12 more


Note that the console.fxml file is in the same package as app.fxml (in both the IDE and the .jar file).
That's why I also tried source="./console.fxml", but nothing changed.










share|improve this question













I am trying to include an fxml file in another one and deploy my application as a runnable jar.



The top-level fxml file is loaded like this:



URL location = Editor.class.getClassLoader().getResource("view/app.fxml");
FXMLLoader fxmlLoader = new FXMLLoader(location);
Parent root = fxmlLoader.load();
appController = fxmlLoader.getController();


app.fxml, which is located in "src/view" contains this line:



<fx:include fx:id="console" source="console.fxml" />


Running this in eclipse will work as expected, but running the exported .jar file will print:



Exception in Application start method
Exception in thread "main" java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:61)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: javafx.fxml.LoadException:
view/app.fxml:92

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2625)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2603)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2435)
at de.hsa.dice.editor.Editor.start(Editor.java:49)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
... 1 more
Caused by: java.net.MalformedURLException: Could not open InputStream for URL 'rsrc:console.fxml'
at org.eclipse.jdt.internal.jarinjarloader.RsrcURLConnection.getInputStream(RsrcURLConnection.java:49)
at java.base/java.net.URL.openStream(URL.java:1117)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2465)
at javafx.fxml.FXMLLoader.access$2700(FXMLLoader.java:105)
at javafx.fxml.FXMLLoader$IncludeElement.constructValue(FXMLLoader.java:1154)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:754)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2722)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2552)
... 12 more


Note that the console.fxml file is in the same package as app.fxml (in both the IDE and the .jar file).
That's why I also tried source="./console.fxml", but nothing changed.







java url include fxml executable-jar






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 18 '18 at 20:17









EzPizzaEzPizza

214




214












  • nobody, really? I thought this might be a common scenario... because how else would anyone use the include tag in a production environment?
    – EzPizza
    Nov 23 '18 at 23:41


















  • nobody, really? I thought this might be a common scenario... because how else would anyone use the include tag in a production environment?
    – EzPizza
    Nov 23 '18 at 23:41
















nobody, really? I thought this might be a common scenario... because how else would anyone use the include tag in a production environment?
– EzPizza
Nov 23 '18 at 23:41




nobody, really? I thought this might be a common scenario... because how else would anyone use the include tag in a production environment?
– EzPizza
Nov 23 '18 at 23:41












1 Answer
1






active

oldest

votes


















0














I found the reason and a workaround for this issue.



The javafx.fxml.FXMLLoader class uses this URL constructor, when creating the FXMLLoader for included fxml files:



public URL(URL context, String spec)


using the location from the FXMLLoader(location) constructor as context and using the "source" from the include-element as spec.



So when loading my root fxml file with the path "view/app.fxml", the context will be the "view" package in my IDE, while it will be the root of the classpath in the exported .jar.



I tried creating the app-URL with



URL classpath = getClass().getProtectionDomain().getCodeSource().getLocation();
URL location = new URL(classpath, "view/app.fxml");


but my context will not be used again when this is plugged into the same constructor again later by the FXMLLoader.



So the only workaround I could think of is to just put all fxml-files (at least those with an include tag) into the root of a source-folder. That way, it will be located directly at the classpath in any environment.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53365036%2finclude-fxml-throws-exception-from-jar%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I found the reason and a workaround for this issue.



    The javafx.fxml.FXMLLoader class uses this URL constructor, when creating the FXMLLoader for included fxml files:



    public URL(URL context, String spec)


    using the location from the FXMLLoader(location) constructor as context and using the "source" from the include-element as spec.



    So when loading my root fxml file with the path "view/app.fxml", the context will be the "view" package in my IDE, while it will be the root of the classpath in the exported .jar.



    I tried creating the app-URL with



    URL classpath = getClass().getProtectionDomain().getCodeSource().getLocation();
    URL location = new URL(classpath, "view/app.fxml");


    but my context will not be used again when this is plugged into the same constructor again later by the FXMLLoader.



    So the only workaround I could think of is to just put all fxml-files (at least those with an include tag) into the root of a source-folder. That way, it will be located directly at the classpath in any environment.






    share|improve this answer


























      0














      I found the reason and a workaround for this issue.



      The javafx.fxml.FXMLLoader class uses this URL constructor, when creating the FXMLLoader for included fxml files:



      public URL(URL context, String spec)


      using the location from the FXMLLoader(location) constructor as context and using the "source" from the include-element as spec.



      So when loading my root fxml file with the path "view/app.fxml", the context will be the "view" package in my IDE, while it will be the root of the classpath in the exported .jar.



      I tried creating the app-URL with



      URL classpath = getClass().getProtectionDomain().getCodeSource().getLocation();
      URL location = new URL(classpath, "view/app.fxml");


      but my context will not be used again when this is plugged into the same constructor again later by the FXMLLoader.



      So the only workaround I could think of is to just put all fxml-files (at least those with an include tag) into the root of a source-folder. That way, it will be located directly at the classpath in any environment.






      share|improve this answer
























        0












        0








        0






        I found the reason and a workaround for this issue.



        The javafx.fxml.FXMLLoader class uses this URL constructor, when creating the FXMLLoader for included fxml files:



        public URL(URL context, String spec)


        using the location from the FXMLLoader(location) constructor as context and using the "source" from the include-element as spec.



        So when loading my root fxml file with the path "view/app.fxml", the context will be the "view" package in my IDE, while it will be the root of the classpath in the exported .jar.



        I tried creating the app-URL with



        URL classpath = getClass().getProtectionDomain().getCodeSource().getLocation();
        URL location = new URL(classpath, "view/app.fxml");


        but my context will not be used again when this is plugged into the same constructor again later by the FXMLLoader.



        So the only workaround I could think of is to just put all fxml-files (at least those with an include tag) into the root of a source-folder. That way, it will be located directly at the classpath in any environment.






        share|improve this answer












        I found the reason and a workaround for this issue.



        The javafx.fxml.FXMLLoader class uses this URL constructor, when creating the FXMLLoader for included fxml files:



        public URL(URL context, String spec)


        using the location from the FXMLLoader(location) constructor as context and using the "source" from the include-element as spec.



        So when loading my root fxml file with the path "view/app.fxml", the context will be the "view" package in my IDE, while it will be the root of the classpath in the exported .jar.



        I tried creating the app-URL with



        URL classpath = getClass().getProtectionDomain().getCodeSource().getLocation();
        URL location = new URL(classpath, "view/app.fxml");


        but my context will not be used again when this is plugged into the same constructor again later by the FXMLLoader.



        So the only workaround I could think of is to just put all fxml-files (at least those with an include tag) into the root of a source-folder. That way, it will be located directly at the classpath in any environment.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 25 '18 at 14:28









        EzPizzaEzPizza

        214




        214






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53365036%2finclude-fxml-throws-exception-from-jar%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to change which sound is reproduced for terminal bell?

            Can I use Tabulator js library in my java Spring + Thymeleaf project?

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents