Goku
Goku

Reputation: 41

Can't read input file - Reading an image contained in a jar file - java

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.

Upvotes: 4

Views: 7060

Answers (3)

Jakub Zaverka
Jakub Zaverka

Reputation: 8874

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).

Upvotes: 1

Michael
Michael

Reputation: 2726

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")));

Upvotes: 0

akappa
akappa

Reputation: 10480

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);

Upvotes: 0

Related Questions