无法读取输入文件 - 读取 jar 文件中包含的图像 - java

发布于 2024-11-07 19:37:35 字数 589 浏览 0 评论 0原文

我想类似的问题已经被问过,但我找不到解决方案。我正在开发一个基于 Java Swings 的绘画应用程序。 我需要为 BufferedImage 类创建一个实例,以便保存我绘制的图像。我想选择一张背景图片,在上面画一些东西。我有一些预定义的背景图像集,我将它们放置在与源文件相同的文件夹中,并在代码中访问这些图像。该代码在 Netbeans 中运行时可以完美运行。如果我构建并运行 jar 文件,它会显示无法读取输入文件。我发现我们无法直接访问 jar 中的文件,因此我使用了以下代码:

InputStream is = this.getClass().getClassLoader().getResourceAsStream("bg3.jpg");
bgfile=ImageIO.read(is);

bgfile 是 BufferedImage 类的对象。因此,一旦我作为 jar 文件运行,就会抛出以下 IO 异常:

无法读取输入文件!

但是如果我用winrar打开jar文件,我可以在jar文件的根目录中找到文件bg3.jpg。 如果我将路径指定为“/bg3.jpg”,则 jar 文件本身不会打开。

所以我需要一些帮助和解释。提前致谢。

I guess similar kind of question has been asked, but I could not find the solution. I am just working on a painting application based on Java Swings.
I need to create an instance for the BufferedImage class so as to save the image that I have drawn. I would like to chose a background picture over which I would draw something. I have some predefined set of background images that I have placed in the same folder as that of the source file and access those in the code. The code works perfectly when it is run in Netbeans. If I build and run the jar file, it says Can't read input file. I came to know that we cannot access the file in jar directly and so I used the following code :

InputStream is = this.getClass().getClassLoader().getResourceAsStream("bg3.jpg");
bgfile=ImageIO.read(is);

bgfile is an object of the BufferedImage class. So Once I run as a jar file, the following IOexception is thrown :

Can't read input file!

But If I open the jar file with winrar, I can find the file bg3.jpg in the root of the jar file.
If I give the path as "/bg3.jpg", the jar file itself is not opening.

So I need some help and explanation on this. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你不是我要的菜∠ 2024-11-14 19:37:35

jar 文件的内容区分大小写。这听起来可能很愚蠢,但是你检查过文件盒吗?如果您有文件 File.jpg,则加载 file.jpg 将不起作用。甚至扩展名也区分大小写(file.JPG 与 file.jpg)。

Contents of a jar file are case-sensitive. This may sound dumb, but did you check the file case? If you have file File.jpg, then loading file.jpg won't work. Even extensions are case-sensitive (file.JPG vs file.jpg).

风月客 2024-11-14 19:37:35

由于您的解决方案与此处解释的相同,我认为您只是忘记将包包含在路径中,即:

InputStream is = this.getClass().getClassLoader().getResourceAsStream("package1/package2/.../bg3.jpg");
bgfile=ImageIO.read(is);

Since your solution is the same as explained here, I think you're just forgetting to include the package in the path, i.e.:

InputStream is = this.getClass().getClassLoader().getResourceAsStream("package1/package2/.../bg3.jpg");
bgfile=ImageIO.read(is);
始于初秋 2024-11-14 19:37:35

完整的堆栈跟踪可能会有帮助。但是,如果您对异常的描述准确,它并不会告诉您找不到图像文件。它告诉你它无法读取它。您确定 jpg 文件没有损坏或者由于某些其他原因 ImageIO 类无法将其识别为 jpg 吗?

如果资源为 null,则 ImageIO.read() 方法会给出异常,原因是 input == null。我认为它正在找到它。它只是无法将其读取为 jpg 文件(尽管在没有完整堆栈跟踪的情况下很难确定)。

如果您的图像资源确实位于 jar 文件的顶层,那么您不需要任何前导文件分隔符。因此,如果您的 jar 看起来像这样:

1523 Tue Mar 30 23:14:50 CDT 2010 org/apache/log4j/xml/SAXErrorHandler.class<br>
286 Tue Mar 30 23:14:50 CDT 2010  org/apache/log4j/xml/UnrecognizedElementHandler.class<br>
4109 Tue Mar 30 23:14:52 CDT 2010 org/apache/log4j/xml/XMLLayout.class<br>
 745 Tue Mar 30 23:14:52 CDT 2010 org/apache/log4j/xml/XMLWatchdog.class<br>
7028 Tue Mar 30 23:14:34 CDT 2010 org/apache/log4j/xml/log4j.dtd<br>
 911 Wed May 11 14:31:30 CDT 2011 redball.gif

您可以使用以下命令加载 redball.gif:

Image image = (ImageIO.read(this.getClass().getClassLoader().getResource("redball.gif")));

The full stack trace would have probably been helpful. However, if your description of the exception is accurate, it isn't telling you it can't find the image file. It is telling you it can't read it. Are you sure the jpg file isn't corrupt or for some other reason isn't recognizable as a jpg to the ImageIO class?

The ImageIO.read() method gives an exception with a cause of input == null if the resource is null. I think it is finding it. It just can't read it as a jpg file (although hard to be certain without the full stack trace).

If your image resource is indeed at the top-level of the jar file, then you don't need any leading file separator. So if your jar looks like this:

1523 Tue Mar 30 23:14:50 CDT 2010 org/apache/log4j/xml/SAXErrorHandler.class<br>
286 Tue Mar 30 23:14:50 CDT 2010  org/apache/log4j/xml/UnrecognizedElementHandler.class<br>
4109 Tue Mar 30 23:14:52 CDT 2010 org/apache/log4j/xml/XMLLayout.class<br>
 745 Tue Mar 30 23:14:52 CDT 2010 org/apache/log4j/xml/XMLWatchdog.class<br>
7028 Tue Mar 30 23:14:34 CDT 2010 org/apache/log4j/xml/log4j.dtd<br>
 911 Wed May 11 14:31:30 CDT 2011 redball.gif

You would load redball.gif with:

Image image = (ImageIO.read(this.getClass().getClassLoader().getResource("redball.gif")));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文