Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Why am I getting a ClassCastException?

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can (and generally do) use the same classloader in multiple threads, yes.
 
Marshal
Posts: 74654
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 74654
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22568
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Done
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic