Getting JAI JPEG2000 to run on Glassfish server without a NPE

If you are using JPEG2000 images you will need the JAI jars to decode them. Although these projects appear essentially dead, they are still needed for JPEG2000 decoding in Java. They run fine on your test machine but put them on a Java EE server such as  Glassfish (or Tomcat) and you get a null pointer exception. What a nightmare! Well, here is a solution if you have this issue….

If you try this code,

BufferedImage img = ImageIO.read(new File(“image.jp2″));

this code will throw exception when it is try to execute below command even if you have the JPEG2000 JAI jar on the classpath (you are using the version where we fixed the memory leak, aren’t you?).

I have drilled down and this is the code which is actually failing. JAI has a powerful, expendable lookup mechanism which works brilliantly (so long as it works).

iter = theRegistry.getServiceProviders(ImageInputStreamSpi.class,
true);

What the code above does is it looks the spi(such as jpeg2000 spi) in services folder under meta-inf files and if it is found then which allocate lookup table for the classes to execute.

for example this is another code which look for png supported buffered image classes

iir = ImageIO.getImageReadersByFormatName(“png”).next();

in both cases you need Buffered image in order to process or convert the image into another format.

if you are using JAI JPEG2000 library you can access buffered image using different mechanism

here is the code what iam using to access the buffered image using J2K library

public BufferedImage getJPEG2000Image(byte[] data){
 
	ImageInputStream iis = null;
	BufferedImage image=null;
	try {
		iis = ImageIO.createImageInputStream(new ByteArrayInputStream(data));
		com.sun.media.imageioimpl.plugins.jpeg2000.J2KImageReaderSpi j2kImageReaderSpi = new com.sun.media.imageioimpl.plugins.jpeg2000.J2KImageReaderSpi();
		com.sun.media.imageioimpl.plugins.jpeg2000.J2KImageReader j2kReader = new com.sun.media.imageioimpl.plugins.jpeg2000.J2KImageReader(j2kImageReaderSpi);
		j2kReader.setInput(iis, true);
		image = j2kReader.read(0, new com.sun.media.imageio.plugins.jpeg2000.J2KImageReadParam());
	}
	catch (Exception e){
		e.printStackTrace();
	}
	return image;
}

i have used fully qualified class names in order to reduce your effort to find and allocate the classes accurately.

I hope this would be helpful for you to use multiple instances of JPEG2000 objects.

This post is part of our “GlassFish Articles Index” series. In these articles, we aim to explore Glassfish in different ways, from useful tutorials, to JaveOne and general.

The following two tabs change content below.

suda

Java EE developer at IDRSolutions
Suda is the Senior Java EE Developer at IDR Solutions, and specialises in XFA, Fonts, True-Type Fonts, application servers and conversions. He is a keen science-fiction fan in his spare time.

Related Posts:

suda

About suda

Suda is the Senior Java EE Developer at IDR Solutions, and specialises in XFA, Fonts, True-Type Fonts, application servers and conversions. He is a keen science-fiction fan in his spare time.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>