question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ImageIO.getImageWriter(reader) not working inside tomcat

See original GitHub issue

Hello Harald,

I wrote a basic test code that does work in a standalone test class:

public class Test {

public static void main(String[] arg)
{
    try
    {
        ImageInputStream iis = ImageIO.createImageInputStream(new FileInputStream(arg[0]));
        Iterator<ImageReader> readers = ImageIO.getImageReaders(iis);
        if (!readers.hasNext())
            throw new IOException("can't decode this image inputstream");

        ImageReader reader = readers.next();
        ImageReadParam param = reader.getDefaultReadParam();
        reader.setInput(iis, true, true);
        BufferedImage img = reader.read(0, param);

        ImageWriter writer = ImageIO.getImageWriter(reader);

        if (writer == null)
            throw new IOException("can't find writer, reader provider: " + reader.getOriginatingProvider());

        System.out.println("Writer: " + writer.getClass());
    }
    catch (Throwable t)
    {
        t.printStackTrace();
    }
}

}

The same code doesn’t work inside a tomcat webapp, writer is null so an exception is thrown.

I have:

  • added the listener as described in the guide
  • fixed class name typos inside JPEGImageWriterSpi (class names were not starting with “com”

I have verified with a test loop that the ImageWriter is loaded:

static
{
    System.out.println("ImageIO, looking for writers");

    Iterator<ImageWriter> ite = ImageIO.getImageWritersByFormatName("jpeg");

    while (ite.hasNext())
        System.out.println("ImageIO writer: " + ite.next().getClass());
}

And the result:

logs/catalina.out:ImageIO, looking for writers logs/catalina.out:ImageIO writer: class com.twelvemonkeys.imageio.plugins.jpeg.JPEGImageWriter logs/catalina.out:ImageIO writer: class com.sun.imageio.plugins.jpeg.JPEGImageWriter

Any idea about why is not working just inside tomcat? How to debug further?

Thanks,

Daniele.

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

7reactions
kkoflercommented, Aug 30, 2017

If, like me, you are still stumbling upon this old issue, and wonder what the best fix/workaround is these days:

  • You want to use the com.twelvemonkeys.servlet.image.IIOProviderContextListener (that is documented on the https://haraldk.github.io/TwelveMonkeys/ front page under “Deploying the plugins in a web app”).
  • To use this in a Maven project, you need this additional dependency:
    <dependency>
      <groupId>com.twelvemonkeys.servlet</groupId>
      <artifactId>servlet</artifactId>
      <version>3.3.2</version>
    </dependency>
  • To use it in a Spring Boot project without a web.xml file, you want to add the following line to the onStartup method in your SpringBootServletInitializer subclass:
servletContext.addListener(IIOProviderContextListener.class);

of course with the following import:

import com.twelvemonkeys.servlet.image.IIOProviderContextListener;

With this, the standard ImageIO classes just work and you do not have to manually load any classes.

I hope this helps.

1reaction
stephanie-dmcommented, Apr 26, 2021

It would be great if @kkofler 's solution could be added to the documentation, because I think it costs people some time to find this…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Java ImageIO suddenly stops working on Tomcat 7
We have a servlet running under tomcat 7 that converts stored TIFF images into JPEGs for display in a web application using the...
Read more >
ImageIO (Java Platform SE 7 ) - Oracle Help Center
This method is provided principally for symmetry with getImageWriter(ImageReader) . Note that this method returns the "preferred" reader, which is the first in...
Read more >
FAQ / Known Issues - Apache Software Foundation
I'm using the Java ImageIO to dynamically serve images and get strange Exceptions from time to time. Is this a bug in Tomcat?...
Read more >
Java Examples for javax.imageio.spi.ImageWriterSpi
WithWriter getImageWriter(final Tile tile, final RenderedImage image, ... 2) (to be run if the above strategy did not found a suitable writer), ...
Read more >
ImageIO (GNU Classpath 0.95 Documentation)
getImageWriter (ImageReader reader): Retrieve an image writer corresponding to an image reader, or null if reader is not registered or if no corresponding ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found