java.io.FileNotFoundException当图像文件在jar中使用jxl.write.WritableImage时 [英] java.io.FileNotFoundException when image file is inside jar using jxl.write.WritableImage

查看:721
本文介绍了java.io.FileNotFoundException当图像文件在jar中使用jxl.write.WritableImage时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using WritableImage to write image into xls file, when I run it inside eclipse it is working fine. But when I run it in executable jar, I getting FileNotFoundException.

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

图像确实在jar内。 C:\scheduler-1.0-SNAPSHOT-jar-with-dependencies.jar \ img \abouts.png

The image is indeed inside the jar. C:\scheduler-1.0-SNAPSHOT-jar-with-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)


推荐答案

一旦图像是一个罐子,它就是嵌入式资源,<$ c无法访问$ C>文件。通常我们会使用接受URL的方法调用或构造函数,但正在使用的API不支持它。

Once the image is an a jar it is an embedded-resource that will not be accessible by File. Normally we would use a method call or constructor that accepts URL, but the API being used does not support that.

所以你可以使用 new WritableImage(double,double,double,double,byte []) 其中 byte [] 是图像的字节数。

So instead you might use new WritableImage(double,double,double,double,byte[]) where the byte[] is the bytes of the image.

这篇关于java.io.FileNotFoundException当图像文件在jar中使用jxl.write.WritableImage时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆