This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Java in General and the fly likes Casting gone Wild - ClassCastException Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Casting gone Wild - ClassCastException" Watch "Casting gone Wild - ClassCastException" New topic
Author

Casting gone Wild - ClassCastException

Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Hey there,

When would the following happen?

if line 2 produces "class com.the.class", under what circumstances would line 4 throw a ClassCastException ?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24081
    
  15

The object pointed to by "t" was loaded by a different class loader than the current context class loader (i.e., generally, by a different one than the one that loaded the code we're looking at.) That other loader also has its own copy of com.the.class . Finally, com.the.class isn't accessible to the system class loader, and the class we're looking at wasn't loaded by the system class loader, either.
[ June 22, 2005: Message edited by: Ernest Friedman-Hill ]

[Jess in Action][AskingGoodQuestions]
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

blaow!!!

DUH. Yah, that was it. How many years doing web-apps and **knowing** about that particular classloader thingy??? argh.

The "code we're looking at" is in one webapp and it's referencing Threads (t) started in another webapp.

I still think it's "kinda funny" about the getClass() result and then failing the cast. It brings to mind that good ole' Jedi Mind trick "These aren't the classes you're looking for"...
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Yah.. and now I realize that my plans to be able to 'monitor' a thread without having to learn about JMX is right out the window.

I was going to have all my daemon threads implement a common 'DaemonThread' interface with methods like "getQueueCount" and "getStatus" and "shutDownRightNow" and such. Each of my webapps starts a number of these threads.

Then I'd have a single status application that was gonna be the 'christmas tree' with green/orange/red indicators.

But I won't be able to cast the Thread returned from code that starts:
Thread ct = Thread.currentThread();
ThreadGroup tg = ct.getThreadGroup();
// etc...

drat!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24081
    
  15

Well, you still have a chance if you can get the common classes loaded by a higher-up class loader (a common ancestor to the two web app's loaders.) Under (say) Tomcat, I think you may be able to do this just by putting the jar in the "common" directory.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Casting gone Wild - ClassCastException
 
Similar Threads
10->8->2
When does Class Cast Exception occur?
Need more info on toArray(Object[])
Inner Class