Q: Even after adding the jai_imageio.jar and jai_codec.jar in the classpath, I am still getting an error when trying to save as Tiff and getting the “No TIFF Writers Available” exception. Any idea?
A: There are some J2EE servers (such as Websphere?) that do not find ImageIO plugins automatically. Try any of the 2 calls below.
1. Scan for Plugins
Try adding this line of code at initialization:
ImageIO.scanForPlugins(); |
2. Register Service Providers
If this still doesn’t work, some users have reported that they had to make calls to register the TIFF reader and writer:
IIORegistry registry = IIORegistry.getDefaultInstance(); registry.registerServiceProvider(new com.sun.media.imageioimpl.plugins.tiff.TIFFImageWriterSpi()); registry.registerServiceProvider(new com.sun.media.imageioimpl.plugins.tiff.TIFFImageReaderSpi()); |
Sample code to check that imageio is loaded
You can use the code below to make sure that imageio was loaded properly and that a TIFF writer was found.
package test; import java.util.Iterator; import javax.imageio.ImageIO; import javax.imageio.ImageWriter; public class TestJPEG2000 { public static void main(String [] args) { try { Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("JPEG2000"); if (writers.hasNext()) { System.out.println("Found writers for JPEG2000."); } else { System.out.println("Did not find writers for JPEG2000."); } } catch(Throwable t) { t.printStackTrace(); } } } |
Method Detail for scanForPlugins
Scans for plug-ins on the application class path, loads their service provider classes, and registers a service provider instance for each one found with the IIORegistry.This method is needed because the application class path can theoretically change, or additional plug-ins may become available. Rather than re-scanning the classpath on every invocation of the API, the class path is scanned automatically only on the first invocation. Clients can call this method to prompt a re-scan. Thus this method need only be invoked by sophisticated applications which dynamically make new plug-ins available at runtime.
The getResources method of the context ClassLoader is used locate JAR files containing files named META-INF/services/javax.imageio.spi.classname, where classname is one of ImageReaderSpi, ImageWriterSpi, ImageTranscoderSpi, ImageInputStreamSpi, or ImageOutputStreamSpi, along the application class path.
The contents of the located files indicate the names of actual implementation classes which implement the aforementioned service provider interfaces; the default class loader is then used to load each of these classes and to instantiate an instance of each class, which is then placed into the registry for later retrieval.
The exact set of locations searched depends on the implementation of the Java runtime enviroment.