1

I tried to add 3 pictures to my program, but when i add pictures, created .jar file cannot start.

When I try clean and build without pictures the .jar file works well.

The pictures path: D:\Desktop\Password\src\resources

final public ImageIcon picture = new ImageIcon(getClass().getResource("./resources/picture.png"));
final public ImageIcon picture1 = new ImageIcon(getClass().getResource("./resources/picture1.jpg"));
final public ImageIcon picture2  = new ImageIcon(getClass().getResource("./resources/picture2.jpg"));

From Netbeans the icons are visible, and a program works correctly.

What did I do wrong?

Tunaki
  • 132,869
  • 46
  • 340
  • 423
  • Possible duplicate of [Add image to JAR Java](http://stackoverflow.com/questions/1096398/add-image-to-jar-java) – Paolo Forgia Aug 29 '16 at 08:50
  • how did you created the jar file, if it is a maven generated artifact , you need to use the maven resource plugin properly because your resource path is not the maven default resource path. – hunter Aug 29 '16 at 08:51
  • Where are your pictured located in your JAR? See http://stackoverflow.com/questions/31127/java-swing-displaying-images-from-within-a-jar – Tunaki Aug 29 '16 at 08:55
  • Probably a wrong resource path. If /resources is top-level in you jar, leave the "." and use getResource("/resources/picture_whatever") – mtj Aug 29 '16 at 08:57
  • My jar file stored: D:\Desktop\Password\dist\Password.jar –  Aug 29 '16 at 08:58
  • I tried without "." When I clik jar file nothing happens. The program can not start. –  Aug 29 '16 at 09:10
  • @BirosAndrásDániel If you look inside your JAR file using something like winrar/7zip then you can check where the images are stored. Then you can adjust the resource strings to match this. --- Also, if you run the JAR file from the command line using `java -jar Password.jar` then you will see the errors. – byxor Aug 29 '16 at 09:16
  • I checked it with 7 zip. The program contains a resources folder with my images. But when i would like to open .jar file still nothing happens. I tried it also command line with no success.I generate .jar with netbeans clicked clean and build. –  Aug 29 '16 at 10:37
  • 1
    I found a solution. My picture extendsion was .JPG I changed it .jpg, and it works! –  Aug 29 '16 at 11:06

0 Answers0