Search code examples
javafxresourcesclassloader

getResource of Class and Classloader null when running from jar


After reading trough a bunch of Posts regarding the Resource System i'm still running into a wall with this one.

The Scenario:

I'm currently developing a javafx applicationm which should run both as a jar and as an Applet. I'm using the simple trick JavaFX into Swing Applications to wrap my application into a fxpanel.

Both Versions work fine in Eclipse.

But now, both the jar and the Applet run into the same Problem, once the first fx Controller is invoked.

My package structure:

/*
 1. 
    2.
        3.
            controller.
                        EntryPoint.class <-- Main
                        MenuController.class
                        TabsController.class
            ui.
                pane.
                        MainMenu.fxml
                        Startuppane.fxml
                        Tabs.fxml
                note.
                        NoteSearch.fxml
                        NoteView.fxml

 */

//edit: Tree Command from my unpacked jar: Pastebin //edit End

My application does the Following:

First, It loads the RootLayout from EntryPoint.class

MainBorder = ROOTLOADER.load(EntryPoint.class.getResourceAsStream("/1/2/3/ui/pane/StartupPane.fxml"));

And then two Childs, a Menu, and some Tabs.

The Menu Works fine, because the MenuController itself does not load any other childitems.

Now the actual issue Comes in when the TabsController is invoked, and tries to load it's own childitems, such as the NoteView.

    TP  = TABSLOADER.load(EntryPoint.class.getResourceAsStream("/1/2/3/ui/pane/Tabs.fxml")); //Loading The Tabs.

So the TabPane gets loaded, and when the TabsController now tries to load his own Childitems. I always get a NullPointer, in the exported jar or the webapplet.

I tried loading the ChildItems with the following Methods:

NOTE_SEARCH_LOADER.load(TabsController.class.getResourceAsStream("/1/2/3/ui/note/NoteSearch.fxml")); 

Works fine in Eclipse, but once exported causes Unknown Path / NullPointer

NOTE_SEARCH_LOADER.load(TabsController.class.getClassLoader().getResourceAsStream("1/2/3/ui/note/NoteSearch.fxml")); 

Using ClassLoader instead, removed first Slash. Works in Eclipse, once exported causes Unknown path / NullPointer

NOTE_SEARCH_LOADER.load(EntryPoint.class.getResourceAsStream("/1/2/3/ui/note/NoteSearch.fxml"));

Using the EntryPoint.class (main) with Absolute Path. Works in Eclipse, once Exported. Unknown Path / NullPointer when exported.

NOTE_SEARCH_LOADER.load(EntryPoint.class.getClassLoader().getResourceAsStream("1/2/3/ui/note/NoteSearch.fxml"));

Using the ClassLoader from my Main Class, removed first Slash. Works in Eclipse, Unknown Path / NullPointer when exported.

//edit3: I did another Test, copying the Code from the TabController into the EntryPoint. the EntryPoint Version of the Code works even after the Export to a Jar or Applet.

I guess it has something to do, how the Controller is invoked?

//edit End

I am scratching my head, and wondering what else i could Try.

//edit2: The NullPointer Causing FXML can be found at the given Location, when i unpack my fxml.

//edit End

Maybe i'm again just missing something very obvious.

Sinclery

Fabian95qw

List of visited threads:

Get a resource using getResource()

Preferred way of loading resources in Java

URL to load resources from the classpath in Java

How to reference javafx fxml files in resource folder?

How do I use Java getResource() to get a resource from a parent directory?

Why does getResource return null


Solution

  • I just took the easy route after hours of trying, by simply throwing the FXML into the same folder as the class which required that specific fxml.

    So my Structure now Looks like this: Pastebin

    And i'm now once again accessing the Resource file with.

    NOTE_SEARCH_LOADER.load(TabsController.class.getResourceAsStream("NoteSearch.fxml"));
    

    Now both the exported Jar and the Web Applet Version work.

    Sincerly Fabian95qw