-1

我有一个奇怪的问题。我有一个游戏,其中一个图像在“黑暗”(黑色Rect)中在屏幕上反弹,鼠标光标代表一个手电筒在黑暗中发光(减去一个椭圆形)。

现在我有一个.png图像和一个.wav声音文件。我将它们都放在项目文件夹的“资源”文件夹中,并通过文件路径"resources/myImage.png""resources/mySound.wav".

当我在 Eclipse 中运行程序时一切正常(使用 Kepler 顺便说一句);图像显示正常,并且在命令时播放声音,但是一旦我将其导出为 jar 并运行,它会显示程序名称几秒钟,就好像它是一个活动程序一样,但在退出/崩溃后不久. 我打开终端并运行java -jar /Users/MyUsername/Desktop/MyProg.jar它得到相同的结果,但我通过终端中的崩溃日志得到。上面写着:

Aug 23, 2013 4:10:02 PM com.spotlight.TestPane <init>
SEVERE: null
javax.imageio.IIOException: Can't read input file!
    at javax.imageio.ImageIO.read(ImageIO.java:1301)
    at com.spotlight.TestPane.<init>(TestPane.java:62)
    at com.spotlight.Spotlight$1.run(Spotlight.java:32)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:721)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:682)
    at java.awt.EventQueue$3.run(EventQueue.java:680)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:691)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at com.spotlight.TestPane.<init>(TestPane.java:69)
    at com.spotlight.Spotlight$1.run(Spotlight.java:32)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:721)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:682)
    at java.awt.EventQueue$3.run(EventQueue.java:680)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:691)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at com.spotlight.TestPane.move(TestPane.java:169)
    at com.spotlight.TestPane$1.actionPerformed(TestPane.java:51)
    at javax.swing.Timer.fireActionPerformed(Timer.java:312)
    at javax.swing.Timer$DoPostEvent.run(Timer.java:244)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:721)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:682)
    at java.awt.EventQueue$3.run(EventQueue.java:680)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:691)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

作为参考,我读取图像文件的代码是:

BufferedImage img;
//...
try {
        img = ImageIO.read(new File("resources/myImage.png"));
    } catch (IOException ex) {
        Logger.getLogger(Spotlight.class.getName()).log(Level.SEVERE, null, ex);
    }

我导出程序的方式如下:

  • 我右键单击我的项目文件夹并选择“导出”
  • 我选择“Runnable Jar”选项
  • 我选择了正确的启动配置
  • 我浏览到正确的导出目的地 (/Users/MyUsername/Desktop/MyProg.jar)
  • 在“库处理”下,我选择“将所需库提取到生成的 JAR 中”
  • “另存为 ANT 脚本”保持未选中状态
  • 我单击“完成”,jar 生成时没有任何警告

我知道 SO 并不是真的用于发布崩溃日志,但我在这里有点难过,所以任何帮助表示赞赏。


更新:我System.err.println在块中添加了一条语句try/catch,并使用 Unarchiver 提取了 jar 的内容,并且没有出现资源文件夹。如果资源没有导出,那可能是我的问题,但我不知道如何解决。

4

1 回答 1

1

常见问题通常张贴在这里。您正在尝试将资源作为不存在文件的文件来访问。停止。Jar 文件不保存文件,因此不要尝试将其中的任何内容作为文件访问。而是将数据作为资源获取。谷歌这个问题,因为正如我所说,它每周在这里发布几次。

于 2013-08-23T21:42:24.533 回答