Cannot read input file - Reading image contained in jar file - java

I assume a similar question was asked, but I could not find a solution. I'm just working on a Java Swings based drawing application. I need to create an instance of the BufferedImage class in order to save the image that I painted. I would like to select a background image over which I would draw something. I have a predefined set of background images that I placed in the same folder as the source file, and access those from the code. The code works fine when it is running on Netbeans. If I create and run the jar file, it says "Cannot read the input file." I found out that we cannot access the file directly in the jar, 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, as soon as I run the jar file, the following IOexception is thrown:

Unable to read input file!

But if I open the jar file with winrar, I can find the bg3.jpg file in the root jar file. If I specify the path as "/bg3.jpg", the jar file itself does not open.

So I need help and explanation. Thank you in advance.

+4
source share
3 answers

The contents of the jar file are case sensitive. This may sound silly, but have you checked the file? If you have a file File.jpg, then loading file.jpg will not work. Even extensions are case sensitive (file.JPG vs file.jpg).

+1
source

Since your solution is the same as described here , I think you just forgot to include the package in the path, that is:

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

A full stack trace would probably be useful. However, if your description of the exception is accurate, it does not tell you that it cannot find the image file. He tells you that he cannot read it. Are you sure that the jpg file is not corrupted or for some other reason is not recognized as jpg for the ImageIO class?

The ImageIO.read () method throws an exception with a reason for entering == null if the resource is null. I think it finds it. It just can't read it as a jpg file (although it's hard to be sure without a full stack trace).

If your image resource is indeed at the top level of the jar file, then you do not need any leading file separator. Therefore, if your banner 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 downloaded redball.gif with:

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

All Articles