1

There are already heaps of answers to this problem. I have also tried a lot of them. I have not found a solution yet.

I load some images within my project (Swing - ImageIcons). In the run dialog all of them are also displayed in my GUI. But after compiling the program can't be started at all. The error messages are different depending on the procedure.

Lastly, I tried simply loading a File to print the absolute path. This then looked like this:

File f = new File(Loadscreen.class.getResource("../../../../resources/materials/icon.png").getFile());
System.out.println(f.getAbsolutePath());

The console returns a NullPointerException for this:

Console compiled:

Exception in thread "main" java.lang.NullPointerException
        at de.franken.ration.gui.Loadscreen.<init>(Loadscreen.java:43)
        at de.franken.ration.Rationboard.onEnable(Rationboard.java:84)
        at de.franken.ration.Rationboard.main(Rationboard.java:75)

Console Eclipse:

H:\Users\Hinrich\Documents\Java\Rationboard\bin\resources\materials\icon.png

In line 43 I define f.

My tree looks like this:

Rationsboard
L_ src
  L_ de
    L_ franken
      L_ ration
        L_ gui
          L_ Loadscreen.class
  L_ resources
    L_ materials
      L_ icon.png

However, the icon is included in the JAR.

Thanks to all who respond.

//EDIT: I played around a bit more. As long as the resource to be loaded is in the same package, it can be loaded. But if I change the package with ../, the NullPointerException comes up.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
hinrich.
  • 13
  • 3

2 Answers2

1

Use:

this.getClass().getResource("/resources/materials/icon.png"); 

Note the two differences to the approach seen in the question:

  1. this.getClass() will find the context class loader appropriate for application resources.
  2. "/resources/materials/icon.png" the leading / will tell the getResource method to search from the root of the class-path or Jar.

BTW: Don't get files involved at any point. getResource returns an URL and resources in a Jar are not accessible as File objects.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Thank you. All images are loaded now. But how do I handle my ````Files```` that I need for XML? ````(DocumentBuilder) db.parse(File file);```` I transfer this XML as a protected template. Path: ````/resources/files/profile.rat```` – hinrich. Mar 16 '22 at 09:24
  • `(DocumentBuilder) db.parse(File file);` There are many times we should refer to the JavaDocs for a class. This is a good example in that the `parse` method is overloaded to accept many different types of object. E.G. [`DocumentBuilder.parse(uri)`](https://download.java.net/java/early_access/panama/docs/api/java.xml/javax/xml/parsers/DocumentBuilder.html#parse(java.lang.String)). There is another method for a much more generic object - an `InputStream`. – Andrew Thompson Mar 16 '22 at 10:27
0

You can verify, as a first step, that the icon is included within the jar, by running jar -tf file.jar.

Have you tried

File f = new File(Loadscreen.class.getResource("/materials/icon.png").getFile()); System.out.println(f.getAbsolutePath());

assuming that the icon.png is in resources/materials folder?

vanxa
  • 21
  • 1
  • Hello and thanks for the help. I can't use your command because I compile using Eclipse. But I have checked that the icon is in the JAR with a file archiver. I tried to use your path as well. Here, too, I got an error. However, the file can't be at the location, because it is outside the package – hinrich. Mar 15 '22 at 15:30
  • 1
    Personally I would ensure that the whole of the /resources tree goes into the deployment jar at the root. Load with resource path "/resources/materials/icon.png" https://technojeeves.com/index.php/aliasjava1/80-loading-files-as-resources-in-java-with-eclipse – g00se Mar 15 '22 at 16:27
  • Thank you! The frustration has given way to enlightenment. I had imagined other things under relative and absolute path specification. Now it works perfectly. – hinrich. Mar 15 '22 at 17:56