Workaround for Batik’s NoClassDefFoundError/ClassNotFoundException: TruncatedFileException

Batik’s JPEGRegistryEntry that is repsonsible for handling inline JPEGs in SVGs contains a reference to TruncatedFileException, which is specific to the Sun/Oracle JDK (as well as other classes used in the same code).
If you’re running e.g. on OpenJDK, you’ll get a NoClassDefFoundError or ClassNotFoundException: com/sun/image/codec/jpeg/TruncatedFileException (the problem is mentioned but not resolved on the mailing list)

You have several options:

  • use the Sun/Oracle JDK
  • copy the class from the JDK and include it with your application (which is ugly)
  • you override the broken class in batik with a patched one as outlined below

//add this code before you use batik (make sure is runs only once)
//via the lower priority this subclass is registered before JPEGRegistryEntry
//and prevents JPEGRegistryEntry.handleStream from breaking when used on a non Sun/Oracle JDK
JPEGRegistryEntry entry = new JPEGRegistryEntry() {
 
  public float  getPriority() {
      //higher than that of JPEGRegistryEntry (which is 1000)
                return 500;
  }
 
 
  public Filter handleStream(InputStream inIS, ...
      //code from org.apache.batik.ext.awt.image.codec.jpeg.JPEGRegistryEntry#handleStream, replace the code that reads the
      //bufferedImage with BufferedImage image = ImageIO.read(is);
   }
}
 
ImageTagRegistry.getRegistry().register(entry);

This entry was posted in Software. Bookmark the permalink.

Leave a Reply

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

Please solve this little equation for verifying that you\'re human *