1

I have a project and I want to export an runnable jar file. But I have one image and a text file. If I export a file, it is exported without an image and text file.

Code:

ImageIcon icon =  new
ImageIcon(Main.class.getClassLoader().getResource("STM.png"));
win.setIconImage(icon.getImage());

error:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.net.URL.toExternalForm()" because "location" is null
    at java.desktop/javax.swing.ImageIcon.<init>(ImageIcon.java:217)
    at Main.Main.main(Main.java:110)

Files:https://i.stack.imgur.com/onjI4.png

  • https://www.cefns.nau.edu/~edo/Classes/CS477_WWW/Docs/pack_resources_in_jar.html could take a look at this link. might be helpful – YHStan Jul 13 '21 at 12:17
  • Have you checked that the jar contains the images and text? You have to access objects in a jar differently from accessing files. – greg-449 Jul 13 '21 at 12:18
  • Make sure you've included the resources in the [right way](https://technojeeves.com/index.php/aliasjava1/80-loading-files-as-resources-in-java-with-eclipse) – g00se Jul 13 '21 at 12:57
  • g00se, it doesn't work, I have a class in the main package, in the main package there is one image and a text file. In class, I write: Main.class.getClassLoader().getResource("src/Main/settings.txt"); Main.class.getClassLoader().getResource("src/Main/STM.png"); – SunsetNeon34 Jul 13 '21 at 20:16
  • There will not be a `src` in the jar, the getResource path should just be relative to main - see for example [here](https://stackoverflow.com/q/31127/2670892) – greg-449 Jul 14 '21 at 06:58
  • greg449, doesn't work either ImageIcon icon = new ImageIcon(Main.class.getClassLoader().getResource("STM.png")); – SunsetNeon34 Jul 14 '21 at 07:14

0 Answers0