wood burning stoves 2.0*
The moose likes Java in General and the fly likes Why am I getting a ClassCastException? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Why am I getting a ClassCastException?" Watch "Why am I getting a ClassCastException?" New topic
Author

Why am I getting a ClassCastException?

Reinhard Mantey
Greenhorn

Joined: Jan 09, 2010
Posts: 6
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


[Thumbnail for Java-curious-bugs.png]

Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Reinhard Mantey
Greenhorn

Joined: Jan 09, 2010
Posts: 6
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

You can (and generally do) use the same classloader in multiple threads, yes.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37936
    
  22
Too difficult for "beginning". Moving thread.

And always tell us what the thread is about.
Reinhard Mantey
Greenhorn

Joined: Jan 09, 2010
Posts: 6
@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

Joined: Oct 13, 2005
Posts: 37936
    
  22
A title like "Why am I getting a ClassCastException?" would have been a lot better.
Reinhard Mantey
Greenhorn

Joined: Jan 09, 2010
Posts: 6
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

Joined: Oct 27, 2005
Posts: 19651
    
  18

Done


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
 
subject: Why am I getting a ClassCastException?
 
Similar Threads
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
Problem w/shutdown hook thread & serialization
Problem with serialization via shutdown hook thread
Exception when using Nimbus LaF
Need help for substance use