Java 运行 .jar 时出错,但 Eclipse 中没有任何内容

标签 java eclipse image runtime-error

当我尝试从桌面将新完成的程序作为 .jar 文件运行时,它在“请稍候...”屏幕上运行时卡住了,我必须指示我的程序仍在加载。同时,从 Eclipse IDE 运行它,它工作得非常好!运行

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

导致错误,如下所示:

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意味着在类中加载某些静态变量时出现异常。该错误是 ImageIcon 中的 NullPointerException,这意味着我的(许多)静态 ImageIcon 之一被赋予了一个空 URL 来加载。我使用了 getClass().getResource("someFile") 如果没有找到任何内容,它会返回 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;

这是我的项目:

Birthday Card Project Image

在 Eclipse 中一切都工作完美,但是当我将其导出到 .jar 文件并使用 java -jar 运行它时,它给了我一个错误!有人可以帮忙拜托吗?

最佳答案

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

SomeClass.class.getResource("bird_animation/white1.png")

关于Java 运行 .jar 时出错,但 Eclipse 中没有任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11394521/

相关文章:

java - 图片翻转且无法缩放

java - Keycloak:身份代理时不要创建本地用户

java - 在Eclipse中,是否有一个 View 显示当前选中元素的成员

java - 在 Eclipse 中,如何排除某些文件(可能基于 .svn 扩展名或文件名)被复制到输出文件夹?

java - SPNEGO 获取用户详细信息

Java:透明地处理状态更改事件

java - launch4j: java.lang.OutOfMemoryError: 超出 GC 开销限制

python - PILL 图像从 base64 转换后的图像中检索 exif dict 进行编辑并保存错误,dict 返回为 NULL,有什么想法吗?

css - 背景图像大小不会保持 100% 高度

javascript - 使用 Canvas 下载图像会增加文件的大小