2

Ran into a issue today.

abc.ClassA a = (abc.ClassA) request.getsession().getAttribute();

and i get classcast exception here.

abc.ClassA is referenced from a jar file in the project build path.

I also read about the class being loaded with different class loaders and the issue could be becoz of that.

skaffman
  • 398,947
  • 96
  • 818
  • 769
Punith Raj
  • 2,164
  • 3
  • 27
  • 45
  • Show us the full stack trace. – skaffman Mar 08 '12 at 10:10
  • http://stackoverflow.com/questions/826319/classcastexception-when-casting-to-the-same-class – Jayan Mar 08 '12 at 10:12
  • java.lang.ClassCastException: vss.YearMakeModel cannot be cast to vss.YearMakeModel at com.gm.nvls.controller.WidgetController.getMake(WidgetController.java:271) at com.gm.nvls.controller.WidgetController.doPost(WidgetController.java:83) at com.gm.nvls.controller.WidgetController.doGet(WidgetController.java:423) at javax.servlet.http.HttpServlet.service(HttpServlet.java:707) at javax.servlet.http.HttpServlet.service(HttpServlet.java:820) at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227) – Punith Raj Mar 08 '12 at 10:17

1 Answers1

1

Your class was loaded by two different classloaders.
I guess your jar is located twice in the classloaders hierarchy: once somewhere in the parent path (lib of weblogic?) and once at a child (war?)

The best solution is putting a jar only once at classpath.

If this is impossible, you can try to change client-first/client-last behavior of classloaders.

Tip: you can debug it and get the problematic classloaders quite easily. Stop in debugger in the problematic line and then compare request.getsession().getAttribute().getClass().getClassloader() and abc.ClassA.class.getClassloader()

Tarlog
  • 10,024
  • 2
  • 43
  • 67