IIOException

hi!

i’m experiencing a really annoying problem! Using following line to load images:
myImage = javax.imageio.ImageIO.read(new File(filepath));
everything works fine when running it directly.
However, once all is packaged in a jar, i get a IIOException thrown!!! what on hell is going on?!

if your image is in the jar, you should use
myImage = javax.imageio.ImageIO.read( ClassLoader.getSystemResource( “myImage.xxx”));
It will look in classpath, thus in jar.

great! thanks!!! works perfectly!

If you let ImagIO handling the streaming you can run into some problems with pngs occassionally (1.4.x). You can work around that by wrapping that resource into a BufferedInputStream by yourself.


BufferedImage i = ImageIO.read(new BufferedInputStream(getClass().getResourceAsStream(path)));

strange java…
pepe’s solution works fine, also when packed in a jar. However, if the jar is started via webstart, it doesn’t work anymore!!!
I just get:


ava.lang.IllegalArgumentException: input == null!
      at javax.imageio.ImageIO.read(Unknown Source)
...*

*pointing to: myImage = javax.imageio.ImageIO.read( ClassLoader.getSystemResource( “myImage.xxx”));

onyx: i tried what you’ve written but it simply didn’t do anything: nor loaded the pic, nor throw an error!!! ???
(using j2sdk 1.5 beta)

I have never once gotten ImageIO to work with any version of Java. Even when it was inside of LWJGL source and I was trying to work one of their tutorials their Image loader methods would throw an error. I’ve never had any problems, however, with using Toolkit to load my Images and MediaTracker to wait on them.

input == null!

That means the URL, you’ve constructed doesn’t point to a file.

The reason why it worked before is that the files were found outside the jar. Try copying the jar into another directory and check if it still works.

yeah, thanks, it was also maybe a ‘/’ was missing in the front of the path (like: “/mypic.xxx”)

On a side note :

You can pass the URL directly to the imageIO load function.
For custom File or RandomAccessFile etc. you have to go with streams to work with jars also.

A neat trick if you use the nice readInt, readBoolean etc. from RandomAccessFile you can simply wrap the stream like this:

// Create file
DataInputStream file = new DataInputStream(url.openStream());

And voila. You can now readInt etc on the DataInputStream just like you could with RandomAccessFile :slight_smile:

[quote]if your image is in the jar, you should use
myImage = javax.imageio.ImageIO.read( ClassLoader.getSystemResource( “myImage.xxx”));
It will look in classpath, thus in jar.
[/quote]
This will only work in jars. If you’d like to load resources whether they are inside your jar or just in the same dir as your jar you could get the URL like this:

// Try to get URL
URL url = this.getClass().getClassLoader().getResource(FilePath);

// Not in JAR? (Note : If run as class file this will find your files in the same dir as the class file)
if (url == null)
{
// Try to get file
File f = new File(System.getProperty(“user.dir”) + File.separator + FilePath);

  // Check if it exists
  if (f.exists())
  {
        url = f.toURL();
  }

}

// Still not found? Bail
if (url == null)
{
return null;
}

Thats not quite true. Using the class loader will work as long as the resource is present in your classpath. So if you put all your resources for instance in a directory called “resources” and put it in classpath the files will still be picked up.

This might be slightly tidier than worrying about fallbacks.

Kev

[quote]Thats not quite true. Using the class loader will work as long as the resource is present in your classpath. So if you put all your resources for instance in a directory called “resources” and put it in classpath the files will still be picked up.

This might be slightly tidier than worrying about fallbacks.

Kev
[/quote]
You are absolutely right ofcourse. It’s just that I don’t usually use the classpath :slight_smile: