1

By default Maven checks for the resources folder based on Standard Directory Layout . Though there is a problem with the final jar.

enter image description here

It finally adds all the resources outside the src/main folder which is making my JavaFX application crass .

Caused by: java.lang.NullPointerException: Input stream must not be null
        at javafx.graphics/javafx.scene.image.Image.validateInputStream(Unknown Source)
        at javafx.graphics/javafx.scene.image.Image.<init>(Unknown Source)
        at main.java.com.goxr3plus.xr3player.application.tools.InfoTool.getImageFromResourcesFolder(InfoTool.java:734)
        at main.java.com.goxr3plus.xr3player.application.Main.start(Main.java:324)
        at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(Unknown Source)
        at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(Unknown Source)
        at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(Unknown Source)
        at java.base/java.security.AccessController.doPrivileged(Native Method)
        at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(Unknown Source)
        at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
        at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
        at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(Unknown Source)
        ... 1 more

Because i am loading the images from the src/main/resources folder like :

class.getResourceAsStream("/main/resources/image/"+ imageName));

And in the final jar the images folder is not under the main folder . I used to have resources folder outside src folder and everything seemed okay , though i wanted to follow Standard Directory Layout so i moved it . Before everything worked well using :

class.getResourceAsStream("/image/"+ imageName));

This is currently directory layout :

enter image description here


What you suggest ? Should i load the images differently? ....

Only that code works now class.getResourceAsStream("/main/resources/image/"+ imageName));

Can somehow tell Maven how to package the resources folder on the final jar ?


After many restarts and clean project and Maven Update Project ... it seems it was Eclipse related problem ... never mind i will leave this question here as it may be useful to someone in the future :)

GOXR3PLUS
  • 6,877
  • 9
  • 44
  • 93
  • 1
    Note that, in the standard layout, source files should be under `src/main/java`. It looks like you currently have them under `src/main`. Also, at runtime, the root of the classpath would be `src/main/java` and `src/main/resources`. All your resource paths should be relative to `src/main/resources`. So if you have a resource at `src/main/resources/foo/bar/File.txt` the absolute path would be `getResource("/foo/bar/File.txt")`. – Slaw Sep 22 '18 at 12:24
  • @Slaw Code is under `src/main/java/com/goxr3plus/xr3player` and resources are under `src/main/resources` , Eclipse shows it ... in a confusing way . I checked from windows explorer :) – GOXR3PLUS Sep 22 '18 at 12:26
  • If Eclipse shows it in a confusing way, then maybe Maven is also confused. Did you configure anything about these paths? Because the defaults should be fine. – Thilo Sep 22 '18 at 12:29
  • @Thilo Here is something very crazy ... i tried `/image/...` and mvn clean package the final jar works ... but when trying to run the code from Eclipse it throws Null Pointer ... o my god that Eclipse .... – GOXR3PLUS Sep 22 '18 at 12:30
  • @Thilo Okay it seems Eclipse has caching bugs or something now it seems to work but one more thing .... Can you show me how to move a folder from resources under `src/main` in the final jar ? I just need to do that too . – GOXR3PLUS Sep 22 '18 at 12:32
  • 1
    Your Eclipse source path is not set up correctly. It looks like the root is `src`, whereas it should be `src/main/java` (and `src/main/resources` as extra runtime classpath). Try to re-import the Project from the Maven definition. – Thilo Sep 22 '18 at 12:33
  • 1
    Maven will automatically copy everything from `/src/main/resources` into your jar file. – Thilo Sep 22 '18 at 12:34
  • @Thilo Thank you i followed your recommendation and re imported the project in Eclipse . It shows the same layout ... but it works so ... – GOXR3PLUS Sep 22 '18 at 12:37
  • @Thilo i need to move `resources/sound` folder under `src/main/` in the final jar for specific reasons i have . How can i do that :) ? I will accept your answer :) – GOXR3PLUS Sep 22 '18 at 12:38
  • 1
    Maven will automatically copy `src/main/resources/sound` to `/sound` in the jar file. This is the default directory layout. – Thilo Sep 23 '18 at 02:01
  • Let's say we want to change that and copy it under a different folder? – GOXR3PLUS Sep 23 '18 at 09:31

1 Answers1

2

Your jar file looks like the expected standard layout.

If you have src/main/resources/image/imageName.png, it would show up in /image/imageName.png in the jar file (in your screen shot, you do have that directory, so that looks good).

You can load it with class.getResourceAsStream("/image/imageName.png").

Thilo
  • 257,207
  • 101
  • 511
  • 656
  • I don't know where that `main` folder in the Jar is coming from. Try a `mvn clean`, maybe it is a leftover from previous experiments? – Thilo Sep 22 '18 at 12:23
  • Code is under `src/main/java/com/goxr3plus/xr3player` and resources are under `src/main/resources` ... trying `src/main/resources/image/....` i am getting `NullPointerException: Input stream must not be null` this is so so strange ... it should work but it doesn't ... – GOXR3PLUS Sep 22 '18 at 12:27
  • It used to worked so well with `/image/...` when i had resources folder outside src. – GOXR3PLUS Sep 22 '18 at 12:28