The Source for Java Technology Collaboration

Home » java.net Forums » JDK » Java SE

Thread: Uncatchable error loading image from URL

Welcome, Guest Help
Login Login
Guest Settings Guest Settings
This question is not answered. Helpful answers available: 2. Correct answers available: 1.

Reply to this Thread Reply to this Thread Search Forum Search Forum Back to Thread List Back to Thread List

Permlink Replies: 3 - Last Post: Sep 10, 2007 2:04 AM by: tarbo
sijedehaan

Posts: 4
Uncatchable error loading image from URL
Posted: Sep 8, 2007 3:28 AM
 
  Click to reply to this thread Reply

I keep my images in a jarfile and load them as followes:

Image image = null;
try
{
image = Toolkit.getDefaultToolkit().getImage( url );
}
catch ( Exception e )
{
// inform user.
}

This works fine, but when i accidently misspell the image name I get:

java.lang.IllegalStateException: zip file closed
at java.util.zip.ZipFile.ensureOpen(Unknown Source)
at java.util.zip.ZipFile.getEntry(Unknown Source)
at java.util.jar.JarFile.getEntry(Unknown Source)
at com.sun.deploy.cache.CachedJarFile.getEntry(Unknown Source)
at sun.net.www.protocol.jar.JarURLConnection.connect(Unknown Source)
at sun.plugin.net.protocol.jar.CachedJarURLConnection.connect(Unknown Source)
at sun.plugin.net.protocol.jar.CachedJarURLConnection.getInputStream(Unknown Source)
at sun.awt.image.URLImageSource.getDecoder(Unknown Source)
at sun.awt.image.InputStreamImageSource.doFetch(Unknown Source)
at sun.awt.image.ImageFetcher.fetchloop(Unknown Source)
at sun.awt.image.ImageFetcher.run(Unknown Source)

Why isn't the exception caught?

Sije de Haan,
Netherlands

tarbo

Posts: 425
Re: Uncatchable error loading image from URL
Posted: Sep 9, 2007 3:31 AM   in response to: sijedehaan
 
  Click to reply to this thread Reply

Because the operation is not executed on the same thread, and therefore the exception isn't, either. AFAIK, this is to prevent clogging up the EDT.

If you wish to load images in the same thread, consider using ClassLoader or the javax.imageio package.

sijedehaan

Posts: 4
Re: Uncatchable error loading image from URL
Posted: Sep 10, 2007 1:28 AM   in response to: tarbo
 
  Click to reply to this thread Reply

Thank you for your time. I noticed later that class ImageIcon indeed waits for that thread to finish using MediaTracker.

But shouldn't method getImage() not FIRST check the presence of the image BEFORE starting the thread to load it? And the problem does not only occur with images, any file in the jar has the same problem.

And why is there no problem when the URL is referring to an image folder at the host? The unknown image simply isn't loaded then, but the program continues normally.

Sije

tarbo

Posts: 425
Re: Uncatchable error loading image from URL
Posted: Sep 10, 2007 2:04 AM   in response to: sijedehaan
 
  Click to reply to this thread Reply

> But shouldn't method getImage() not FIRST check the
> presence of the image BEFORE starting the thread to
> load it? And the problem does not only occur with
> images, any file in the jar has the same problem.
>
> And why is there no problem when the URL is referring
> to an image folder at the host? The unknown image
> simply isn't loaded then, but the program continues
> normally.

Those are good questions. Should getImage check for existence first, or is that our responsibility as developers?

I can answer the latter question, however. Recall that, when you send an URL request to a server, Java uses the URLConnection class. This abstract class has an InputStream to represent data sent by the server, which is anything that is not a header. :)

However, what happens when an image isn't present? If HTTP is used, an appropriate header is returned (likely 404). But it's well possible the image is read directly from the inputstream, which will simply contain 0 bytes, resulting in an empty image.

On the other hand, trying to open a stream to an archive that doesn't exist, or to an archive entry that doesn't exist, is likely to cause an IOException instead, because no stream to such a resource can be made. Rather than returning an empty stream, an exception occurs somewhere during execution.

It's in the implementation details, I suppose. For icons and the such, I prefer the "silent failure" technique, since I'll have text ready to replace it with (using Actions). For more "critical" images, I prefer the exception throwing.




 XML java.net RSS