This discussion is archived
3 Replies Latest reply: Jan 8, 2007 11:17 AM by 807607 RSS

ClassCastException very strange

807607 Newbie
Currently Being Moderated
Hello i make the following thinks on websphere 5.1 :

=========================================
INTERFACE
=========================================
public interface DetailCommandeInterface {
public HashMap getDetailCommandes(String[] numerosDossier);
}
=========================================
IMPLEMENTATION ( a singleton )
=========================================
public class DetailCommandeImpl implements DetailCommandeInterface
{
private static DetailCommandeImpl detailcmd;

private DetailCommandeImpl()
{
}

public static DetailCommandeImpl getInstance()
{
if (detailcmd == null)
detailcmd = new DetailCommandeImpl();

return detailcmd;
}

public HashMap getDetailCommandes(String[] numerosDossier) {
return new HashMap();
}
}
=========================================

I've got one ear.

One servlet, in one war of that ear do that on the init :

getServletContext().getContext(IExt.CTX_BOUTIQUE).setAttribute("detailCommande", DetailCommandeImpl.getInstance()) ;

A second servlet, in another war, does that :

DetailCommandeInterface interf = (DetailCommandeInterface)(getServletContext().getContext(IExt.CTX_BOUTIQUE).getAttribute("detailCommande"));

I get this error :

message: fr.laser.boutique.service.externe.impl.DetailCommandeImpl
classe: class java.lang.ClassCastException

Any ideas ?
  • 1. Re: ClassCastException very strange
    796440 Guru
    Currently Being Moderated
    Probably a classloader issue. Right before you do the cast, print out the class of the object (or look at it in a debugger). If the classname is exactly what you expect--including package name and capitalization--then it's definitely a classloader issue.

    I don't know about websphere's class loading, so you'll have to look at its docs or forum for details there. The short version is that if the same class is loaded by two different classloaders, then it's not the same class. You'll need to make sure that class is in ONE place where it can be picked up by all the classloaders that need it.
  • 2. Re: ClassCastException very strange
    796447 Newbie
    Currently Being Moderated
    Piggybacking on what jverd said, you should look at the classloader and class name of both the source (what the object actually is) and target (the class you're trying to cast it to).
    Object o = getServletContext().getContext(IExt.CTX_BOUTIQUE).getAttribute("detailCommande");
    Class c = o.getClass(); // assuming o isn't null here
    System.out.println("Actual class: " + c + ", loaded from " + c.getClassLoader());
    c = DetailCommandeInterface.class;
    System.out.println("Desired class: " + c + "; loaded from " + c.getClassLoader());
  • 3. Re: ClassCastException very strange
    807607 Newbie
    Currently Being Moderated
    jverd , thanks that right ...

    The classloader between my two war are in conflict ( the same interface ).
    I've pushed my interface in a jar that loaded with the ear and delete it of my two war and now this work fine.

    Thanks ;]