• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why am I getting a ClassCastException?

 
Reinhard Mantey
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have an application, where I load jarfiles at runtime. I use URLClassloader to remember the loaded files, to be able to load the complete content of the jarfiles.
All that works fine for most cases ...

but now I get this strange error (see screenshot), with the trace:


The referred code line from StdSkinImage:68 is:


a superclass of StdSkinImage implements ChildEntity<de.schwarzrot.dvd.theme.standard.StdTheme>, which has a access-method
with T being de.schwarzrot.dvd.theme.standard.StdTheme

StdTheme is already instantiated, cause it acts as factory for StdSkinImage.
I guess, that the getParent() causes the problem, but I don't know, how I can track that error down.
When I load the sources in eclipse, no error occurrs. It happens only when I call the application standalone.
Sure, the loading of the jarfile happens in a different thread, but both threads have the right classloader (that contains the jar-urls).

kind regards

Reinhard
Java-curious-bugs.png
[Thumbnail for Java-curious-bugs.png]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is (as you probably already suspect) the kind of error you get when you manage to load a class twice using two different classloaders. This can happen because you've got more than one user-defined classloader, or it can happen if your user-defined classloader doesn't properly delegate to the system class loader. Checking getClass().getClassLoader() on the object you're trying to cast and comparing it with calling the same methods for other objects (or comparing it to references of the actual classloaders you've created, or checking for null, which can represent the system classloader) may help you track down the problem.
 
Reinhard Mantey
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

thank you very much for your attention and support!

I have to confess, I did not suspect the double class loading - I did not ever thought, that it is possible.
So thanks, I gonna look into that direction.

My application starts from a jarfile too, so is it ok, if I set the original classloader as parent of the created URLClassLoader and can I use that URLClassLoader in any thread, or do I have to care about the threads classloader every time?

kind regards
Reinhard
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can (and generally do) use the same classloader in multiple threads, yes.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50176
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Too difficult for "beginning". Moving thread.

And always tell us what the thread is about.
 
Reinhard Mantey
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Ernest Friedman-Hill
Thanks again for your help!

I found lots of bugs around dynamic classloading - I nearly get frustrated yesterday.
But thanks to your hints I could solve it and now it works fine

@Campbell Ritchie
If I would have known my error to build a better thread-title, I probabely did not have to ask for help ;)

kind regards
Reinhard
 
Campbell Ritchie
Sheriff
Pie
Posts: 50176
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A title like "Why am I getting a ClassCastException?" would have been a lot better.
 
Reinhard Mantey
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK - sounds good.

But I'm not allowed to change the thread title. May be you like to change it.

regards
Reinhard
 
Rob Spoor
Sheriff
Pie
Posts: 20661
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Done
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic