• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Explain this ClassCastException, Please

 
Joel McNary
Bartender
Posts: 1824
Eclipse IDE Java Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working on a shopping cart for a company. My JSP pages are simply HTML with java code embedded using <% and %> -- not the most sophisticated, but I'm still learning--and the deadlines too tight for me to go back and redo these now
Anyway, I'm receiving the following error:

The offending lines of code are:

Obviously, the extra verbage was me trying to make sense out of this; so far, it's only served to confuse me. Why would I be getting a ClassCastException when the target object is of the expected class?
This is particularly annoying because this all tested out on my machine fine, but when I moved to the webserver I've been getting all sorts of errors. I just recently noticed that the server is running Tomcat 3.2.3 while I'm running 4.1--But would the difference in the versions account for this
 
Peter Storch
Ranch Hand
Posts: 74
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you check if the class is available more than once in your classpath, e.g. WEB-INF/classes, WEB-INF/lib, <some_server>/lib or on the system classpath?
This error can occour if the same class is loaded by different class loaders.
 
Joel McNary
Bartender
Posts: 1824
Eclipse IDE Java Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't seem to be in multiple places--it only resides in WEB-INF/classes; everything in WEB-INF/lib is eithier things like Java Mail API jars or (my) third party jar files.
 
Craig Jackson
Ranch Hand
Posts: 405
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, that would be my suggestion.
That there is conflict(i.e. multiple versions) on the class:
com.silverlake.database.Order
which is causing your ClassCastException.
Craig
 
Joel McNary
Bartender
Posts: 1824
Eclipse IDE Java Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm...I've been thinking, but correct me if I'm wrong.
Timeline:
1. Develop original application. Upload to Web site. Find some problems that exist only on web server.
2. Make some changes, specifically to the Order class and the cart.jsp file (the one that is giving the error message). Also change the OrderBookServlet, which places the order in the session. UPload these changes.
3. Tomcat detects changes to cart.jsp and recompiles, using new version of class Order that was uploaded. The OrderBookServlet, however, is not reloaded. It is still using the old version of the class.
4. New Version != old version. ClassCastException.
So:
1). How do I check the versions of a class being used so that I can verify this (I guess the question is "Can I check...").
2). Is there anyway to get Tomcat to reload the Servlets short of having to get the server restarted (which requires going through tech support....)?
 
Peter Storch
Ranch Hand
Posts: 74
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joel McNary:

1). How do I check the versions of a class being used so that I can verify this (I guess the question is "Can I check...").

I think you are right with your problem description. If the Servlet container just reloaded the changed classes but there are already used instances of it than this problem seems reasonable. But if you can check this at runtime? Huh, that's a tricky question. I don't think that's possible, but I'm also interessted in a solution.
Originally posted by Joel McNary:
2). Is there anyway to get Tomcat to reload the Servlets short of having to get the server restarted (which requires going through tech support....)?[/QB]

I think in Tomcat 4.x you can restart a single webapp without the need of restarting the whole tomcat. But if you don't have access to the manager app, I think you have to bother the tech support.
 
Ron Newman
Ranch Hand
Posts: 1056
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you delete everything in ${TOMCAT_HOME}/work/Standalone/${hostname}/${appname} , won't that force recompilation and restarting of all JSPs in the app?
 
Peter Storch
Ranch Hand
Posts: 74
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ron Newman:
If you delete everything in ${TOMCAT_HOME}/work/Standalone/${hostname}/${appname} , won't that force recompilation and restarting of all JSPs in the app?

Hmmm, and what about the existing instances of a class "living" in the HTTP session? I don't think the HTTP sessions are destroyed during jsp recompilation. I really think you need to restart the webapp, or wait until all sessions are timedout.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a 'horrible-hack' solution to this problem that involves getting Object streams joined by a Piped stream, passing the offending object in one end, pulling it out the other and doing the cast at that point, but it is much preferable to stop the problem from happening in the first place than patching it.
 
Peter Storch
Ranch Hand
Posts: 74
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by David O'Meara:
There is a 'horrible-hack' solution...

Uhh, that sounds really horrible
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic