1

I am writing a software in java and this inheritance problem always crops up. Funny enough, I don't get problems when i run the program in my IDE (Eclipse Kepler) but once i export it to an executable jar, problem starts. I think I am the only one experiencing this problem because all searches on google show the direct opposite.

My problem is I have an interface named VoteType. This interface is implemented by several concrete classes. I save the concrete implementation to a file using XMLEncoder and retrieve it using XMLDecoder.

But when i try to cast it to the interface it implements VoteType, I get a ClassCastException. What could possibly be causing this?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Staphy
  • 19
  • 1
  • 5

1 Answers1

0

You are not precise enough to track this down, but I assume that the interface and the object implementing the interface and which is created via XMLDecoder are created by different class loaders. Could that be the reason?

See also this problem: Solution for the ClassCastException due to ClassLoader issue

Community
  • 1
  • 1
Christian Fries
  • 16,175
  • 10
  • 56
  • 67
  • Well, i have another class loader which loads plugins but this class loader for the thread causing the problem is the system class loader – Staphy Oct 02 '13 at 19:48
  • Either they need to be loaded from the same classloader, or the base type has to be loaded from a parent classloader of the one that loaded the subtype. How are the classloaders loading XMLEncoder and VoteType related? – nitind Oct 02 '13 at 19:51
  • The system class loader loads both classes. My problem is why it would work fine in eclipse but start misbehaving once it is exported to an executable jar. Even the instanceof operator returns false when i check if(serializedVote instanceof VoteType) – Staphy Oct 02 '13 at 20:14
  • Eclipse might run under a different JVM and/or load a different implementation of XMLDecoder compared to running the executable-jar. Can you please add a System.out.print of the seralizedVote.class and VoteType.class with details about the class loader, etc. – Christian Fries Oct 03 '13 at 19:40