1

当我尝试从我的桌面以 .jar 文件的形式运行我新完成的程序时,它在“请稍候...”屏幕上运行时卡住了,我必须表明我的程序仍在加载。同时,从 Eclipse IDE 运行它可以正常工作!跑步

java -jar C:\path\to\my\jar\file.jar

结果Error如下:

Exception in thread "AWT-EventQueue-0" java.lang.ExceptionInInitializerError
    at ...
Caused by java.lang.NullPointerException
    at javax.swing.ImageIcon.<init>(ImageIcon.java:205)...

的,我知道java.lang.ExceptionInInitializerError这意味着Exception在一个类中加载了一些静态变量。错误是NullPointerExceptionin an ImageIcon,这意味着我的(许多) staticImageIcon中的一个被赋予了要加载的空 URL。如果什么也没找到,我使用getClass().getResource("someFile")which 返回 null 。是的,我已正确拼写所有图像名称。这是我的代码:

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;

这是我的项目:

生日贺卡项目形象

在 Eclipse 中一切都运行良好,但是当我将它导出到 .jar 文件并使用它运行时,java -jar它给了我一个错误!有人可以帮忙

4

1 回答 1

2

检查所有图像是否都在 jar 中,并使用正斜杠而不是反斜杠作为资源路径:

SomeClass.class.getResource("bird_animation/white1.png")
于 2012-07-09T12:14:20.953 回答