suda Senior Java EE Develope specialises in Pdf forms , Fonts, application servers and Image manipulation, meditates in spare time.

Getting JAI JPEG2000 to run on Glassfish server without a NPE

1 min read

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 = 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,

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)); j2kImageReaderSpi = new; j2kReader = new;
j2kReader.setInput(iis, true);
image =, new;
catch (Exception e){
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.

In the end we gave up with trying to patch ImageIO and wrote our own ImageIO replacement from scratch. Lots of other developers seemed to have the problems so we made this commercially available as the JDeli Image library.

Why use JDeli?

JDeli offers a range of advantages over ImageIO and alternatives, including:

  • prevents heap related JVM crashes
  • implements unsupported image formats
  • reduce output file size
  • improve read/write performance
  • supports threading
  • superior image scaling algorithms

IDRsolutions develop a Java PDF library, a PDF forms to HTML5 converter, a PDF to HTML5 or SVG converter and a Java Image Library that doubles as an ImageIO replacement. On the blog our team post about anything interesting they learn about.

suda Senior Java EE Develope specialises in Pdf forms , Fonts, application servers and Image manipulation, meditates in spare time.

One Reply to “Getting JAI JPEG2000 to run on Glassfish server without…”

  1. Hello, this is actually a very good blog and an excellent post, more when there is not much information about how work with JPEG2000 format in Java. Here you post the code to decode an image, but at this moment what I need is the inverse process, is to say, convert an image to JPEG2000, please if you or someone else have done something like that, please let me know, I will thank very much.

Leave a Reply

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

IDRsolutions Ltd 2020. All rights reserved.