无法读取BufferedImage

时间:2013-08-23 21:39:46

标签: java eclipse embedded-resource bufferedimage javax.imageio

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

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

当我在Eclipse中运行程序时(使用Kepler btw),一切正常;图像显示正常,声音在命令时播放,但是一旦我将其导出为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)

对于refence,我的读取图像文件的代码是:

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生成时没有任何警告

我知道这不是真的发布崩溃日志,但我很难过,所以任何帮助都会受到赞赏。


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

1 个答案:

答案 0 :(得分:1)

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