java.io.FileNotFoundException当图像文件在jar中使用jxl.write.WritableImage时

时间:2015-02-22 06:19:30

标签: java swing embedded-resource jxl

我正在使用WritableImage将图像写入xls文件,当我在eclipse中运行它时工作正常。但是当我在可执行jar中运行它时,我得到了FileNotFoundException。

WritableImage image = new WritableImage(0.0D, 0.0D,
      1.0D, 3.0D,
      new File(getClass().getResource("/img/abouts.png").getPath()));

图像确实在罐子里面。 C:\调度-1.0-快照罐与 - dependencies.jar \ IMG \ abouts.png

以下是例外:

java.io.FileNotFoundException: file:C:<some directory>scheduler-1.0-SNAPSHOT-jar-with-dependencies.jar!/scheduler.jar!/img/abouts.png (No such file or directory)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:120)
    at jxl.biff.drawing.Drawing.getImageBytes(Drawing.java:778)
    at jxl.biff.drawing.BlipStoreEntry.<init>(BlipStoreEntry.java:98)
    at jxl.biff.drawing.DrawingGroup.write(DrawingGroup.java:427)
    at jxl.write.biff.WritableWorkbookImpl.write(WritableWorkbookImpl.java:931)
    at com.flagpole.client.IndividualCabinSched.<init>(IndividualCabinSched.java:218)
    at com.flagpole.client.Reports.getIndividualCabinScheReport(Reports.java:948)
    at com.flagpole.client.Reports.actionPerformed(Reports.java:652)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)

1 个答案:

答案 0 :(得分:2)

图片为jar后,File将无法访问。通常我们会使用接受URL的方法调用或构造函数,但是使用的API不支持它。

因此,您可以使用new WritableImage(double,double,double,double,byte[]),其中byte[]是图像的字节。