M for Mite

JAI imageio casting problem

Wednesday Jan 13, 2010

I reacently had problems using Java Advanced Imaging (JAI).

Here is the simplified version of the problem:

 SomeClass c = (SomeClass) obj.getSomeClassParent()

not always but it happens sometimes to trigger exception

 org.somepackage.SomeClass can't be cast to org.somepackage.SomeClass 

Original code

        ImageInputStream iis = ImageIO.createImageInputStream(src);
       
Iterator<ImageReader> iter = ImageIO.getImageReadersByFormatName("DICOM");
       
ImageReader reader = iter.next();
       
DicomImageReadParam param = (DicomImageReadParam) reader.getDefaultReadParam();

And the original exception

 org.dcm4che2.imageio.plugins.dcm.DicomImageReadParam
 can't be cast to  org.dcm4che2.imageio.plugins.dcm.DicomImageReadPara


The problem was that I was missing that sometime the class is loaded by different class loader

and it can happen that the casted class is not the same whit the one form the default class loader.

The question on StackOverflow

Comments:

I have the same issue. I've created a Singleton that returns Spring ApplicationContext. When I redeploy the project (OSGi environment), the ApplicationContext returns objects from the previous ClassLoader and the same exception arises.

Posted by Andon Sikavica on January 14, 2010 at 09:55 PM CET #

Post a Comment:
  • HTML Syntax: Allowed
JavaLogo
JugMKLogo
Support Wikipedia

« February 2012
SunMonTueWedThuFriSat
   
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
   
       
Today

Feeds

Search this blog

Links

Weblog menu

Today's referrers