Reputation:
When I try to run my newly finished program from my desktop as a .jar file it gets stuck while running on the "Please Wait..." screen I have to indicate my program is still loading. At the same time, running it from Eclipse IDE it works perfectly fine! Running
java -jar C:\path\to\my\jar\file.jar
results in an Error
that looks like:
Exception in thread "AWT-EventQueue-0" java.lang.ExceptionInInitializerError
at ...
Caused by java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:205)...
Yes, I know java.lang.ExceptionInInitializerError
means there was an Exception
while loading some static variable in a class. The error was a NullPointerException
in an ImageIcon
, meaning one of my (many) static ImageIcon
's was given a null URL to load. I used getClass().getResource("someFile")
which returns null if nothing was found. Yes, I have spelled all the image names correctly. Here is my code:
public static final ImageIcon cloud = new ImageIcon(SomeClass.class.getResource("cloud.png"));
public static final ImageIcon whiteFlower = new ImageIcon(SomeClass.class.getResource("white_flower.png"));
public static final ImageIcon pinkFlower = new ImageIcon(SomeClass.class.getResource("pink_flower.png"));
public static final ImageIcon redFlower = new ImageIcon(SomeClass.class.getResource("red_flower.png"));
public static final ImageIcon blueFlower = new ImageIcon(SomeClass.class.getResource("blue_flower.png"));
public static final ImageIcon bird1 = new ImageIcon(SomeClass.class.getResource("bird_animation\\bird1.png"));
public static final ImageIcon bird2 = new ImageIcon(SomeClass.class.getResource("bird_animation\\bird2.png"));
public static final ImageIcon bird3 = new ImageIcon(SomeClass.class.getResource("bird_animation\\bird3.png"));
public static final ImageIcon bird4 = new ImageIcon(SomeClass.class.getResource("bird_animation\\bird4.png"));
public static final ImageIcon bird5 = bird4;
public static final ImageIcon bird6 = bird3;
public static final ImageIcon bird7 = bird2;
public static final ImageIcon bird8 = bird1;
public static final ImageIcon moon = new ImageIcon(SomeClass.class.getResource("moon.png"));
public static final ScaledImageIcon star = new ScaledImageIcon(SomeClass.class.getResource("star.png"), 0.225/*average of 0.15 and 0.3*/);
public static final PixelResizeImageIcon squirrel = new PixelResizeImageIcon(SomeClass.class.getResource("squirrel.gif"/*"bunny.gif"*/), 1.5);
public static final ScaledImageIcon whiteBird1 = new ScaledImageIcon(SomeClass.class.getResource("bird_animation\\white1.png"), 1);
public static final ScaledImageIcon whiteBird2 = new ScaledImageIcon(SomeClass.class.getResource("bird_animation\\white2.png"), 1);
public static final ScaledImageIcon whiteBird3 = new ScaledImageIcon(SomeClass.class.getResource("bird_animation\\white3.png"), 1);
public static final ScaledImageIcon whiteBird4 = new ScaledImageIcon(SomeClass.class.getResource("bird_animation\\white4.png"), 1);
public static final ScaledImageIcon whiteBird5 = new ScaledImageIcon(SomeClass.class.getResource("bird_animation\\white5.png"), 1);
public static final ScaledImageIcon whiteBird6 = whiteBird4;
public static final ScaledImageIcon whiteBird7 = whiteBird3;
public static final ScaledImageIcon whiteBird8 = whiteBird2;
public static final ScaledImageIcon whiteBird9 = whiteBird1;
And here is my project:
Everything is working perfectly fine in Eclipse, but when I export it to a .jar file and run it with java -jar
it gives me an error! Can somebody help please?
Upvotes: 1
Views: 458
Reputation: 691715
Check that all the images are in the jar, and use forward slashes rather than backslashes for resource paths:
SomeClass.class.getResource("bird_animation/white1.png")
Upvotes: 2