Question

I am trying to convert PDF to tif images. I use following code to get the image writers by format.

Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("TIFF");

        if (writers == null || !writers.hasNext()) {
            throw new ImageWritingException();
        }

This works fine when I run the application standalone in Eclipse. But when I deploy the application to tomcat server in linux, writers==null is false but !writers.hasNext is true resulting to throw Exception.

I use maven to build the project to war.

I have following dependencies in the pom file.

<dependency>
            <groupId>org.icepdf</groupId>
            <artifactId>icepdf-core</artifactId>
        </dependency>

        <dependency>
            <groupId>com.sun.media</groupId>
            <artifactId>jai_imageio</artifactId>
        </dependency>

        <dependency>
            <groupId>com.sun.media</groupId>
            <artifactId>jai-codec</artifactId>
        </dependency>

        <dependency>
            <groupId>javax.media</groupId>
            <artifactId>jai_core</artifactId>
        </dependency>

What can be the difference between two environments? How can I fix this issue?

Was it helpful?

Solution 2

Tiff support is provided by the Java Advanced Imaging plugin jai_core.jar.

In order to work correctly, the jar file needs to be added to the JVM's ext directory, otherwise it won't register properly

OTHER TIPS

I met the same issue and found the root cause.

Let me summarize first, the issue does not occur in eclipse on dev machine, and it occurs on Tomcat server.

The root cause is that the imageio uses SPI, and there is a basic implementation in JDK (please refer to rt.jar, we can find it with two plugins for bmp and jpeg.) while the plugins we wants are in jai_imageio.jar.

With default configuration, Tomcat scans the one in rt.jar for plugins during initialization for ImageIO. Later when the application runs, the jai_imageio.jar won't be scanned.

As a result the plugins in jai_imageio.jar are not available. When running in dev machine, jai_imageio.jar is scanned.

There are several solutions as listed below, i would recommend the first one as it fits the design intention of ImageIO.

  1. without changing the tomcat default configuration, re-scan the jar.

    static { ImageIO.scanForPlugins(); }

  2. changing the tomcat configuration, so tomcat won't initialize ImageIO. edit file /conf/server.xml,add appContextProtection="false" like following:

<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" appContextProtection="false" />

With suchconfiguration, tomcat won't call ImageIO.getCacheDirectory in JreMemoryLeakPreventionListener, so ImageIO is not initialized until our codes runs.

Got my writer like this :

TIFFImageWriterSpi tiffSpi = new TIFFImageWriterSpi();
ImageWriter imageWriter = tiffSpi.createWriterInstance();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top