Home arrow static arrow Java Programming [Archive] - java.lang.ClassCastException: plugin.tryit ?
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - java.lang.ClassCastException: plugin.tryit ?
This topic has 18 replies on 2 pages.    1 | 2 | Next »

Posts:370
Registered: 8/17/02
java.lang.ClassCastException: plugin.tryit ?  
Jun 24, 2004 6:33 AM



 
OK will someone tell me why in the SAME PETE I am getting a

java.lang.ClassCastException: plugin.tryit

here is they tryit plugin---------------------------------

package plugin;
public class tryit implements plugin.Interfaces.Interface{

/** Creates a new instance of tryit */
public void run()
{
System.out.println("1");
}

public Object HTMLreturn() {
return "hiyyyyyyyyyyy";
}

public void run(Object t) {
}

}

-Located at:------------------------------------------------------
C:\Documents and Settings\happy\Desktop\jakarta-tomcat-5.0.25\webapps\servlets-examples\WEB-INF\classes\plugin\tryit.class

INTERFACE

package plugin.Interfaces;
public interface Interface {

public void run();
public void run(Object t);
public Object HTMLreturn();

}
--------------located at-----------------------
C:\Documents and Settings\happy\Desktop\jakarta-tomcat-5.0.25\webapps\servlets-examples\WEB-INF\classes\plugin\Interfaces\interface.class

when I execute my web engin I get the following error
java.lang.ClassCastException: plugin.tryit

--------------Engin code snippet-----------------------
File x = new File("C:
Documents and Settings\\happy\\Desktop\\jakarta-tomcat-5.0.25\\webapps\\servlets-examples\\WEB-INF\\classes
");
output.print("555");
java.net.URL url[] = new java.net.URL[]{x.toURL()};
output.print("444" + url[0]);
ClassLoader ldr = new java.net.URLClassLoader(url);
output.print("333");
java.lang.Class cls = ldr.loadClass("plugin."+list.next()); //ll.substring((ll.length()) - 6)
//java.lang.Class cls = cls.forName("plugin."+list.next());
output.print("222");
Object gh = cls.newInstance();
output.print("111"+gh);
plugin.Interfaces.Interface xxx = (plugin.Interfaces.Interface) gh; <<< Error occures here
output.print("111"+gh);

located at the base directory

I also have a server up.. .for all to view.. if it assist you in any way
http://206.146.72.38/servlets-examples/servlet/WebEngin?PreviewIndex=1&Plugin0=tryit
I spent a good part of the night trying to figure out why I am getting a java.lang.ClassCastException: plugin.tryit
I am going to bed, I have not slept all night.. I will put this on watch.. someone please throw me a bone..
 

Posts:370
Registered: 8/17/02
Re: java.lang.ClassCastException: plugin.tryit ?  
Jun 24, 2004 6:35 AM (reply 1 of 18)



 
ps it works on non servlet applications it seems
 

Posts:370
Registered: 8/17/02
Re: java.lang.ClassCastException: plugin.tryit ?  
Jun 24, 2004 12:46 PM (reply 2 of 18)



 
bump
 

Posts:6,750
Registered: 1/25/04
Re: java.lang.ClassCastException: plugin.tryit ?  
Jun 24, 2004 12:56 PM (reply 3 of 18)



 
Look at the stack trace and tell us on what line the error occurs.
 

Posts:370
Registered: 8/17/02
Re: java.lang.ClassCastException: plugin.tryit ?  
Jun 24, 2004 12:58 PM (reply 4 of 18)



 
I point out where it occures in the web engin....
 

Posts:370
Registered: 8/17/02
Re: java.lang.ClassCastException: plugin.tryit ?  
Jun 24, 2004 1:01 PM (reply 5 of 18)



 
it does not make any sense.. as far as I am concerned I casted right...
 

Posts:6,750
Registered: 1/25/04
Re: java.lang.ClassCastException: plugin.tryit ?  
Jun 24, 2004 1:01 PM (reply 6 of 18)



 
Ah, I see it now. Before that line do this
System.out.println(gh.getClass().getName());
and see if that gives you any clues.
 

Posts:370
Registered: 8/17/02
Re: java.lang.ClassCastException: plugin.tryit ?  
Jun 24, 2004 1:02 PM (reply 7 of 18)



 
lol it is kinda pathetic.. I load the object... i get an instance.. and I get stuck at casting... how embarressing
 

Posts:370
Registered: 8/17/02
Re: java.lang.ClassCastException: plugin.tryit ?  
Jun 24, 2004 1:08 PM (reply 8 of 18)



 
:-(( that doesn't help at all... it does not show anything....
 

Posts:370
Registered: 8/17/02
Re: java.lang.ClassCastException: plugin.tryit ?  
Jun 24, 2004 1:20 PM (reply 9 of 18)



 
ok I got it working... it says plugin.tryit, still doesn't help me any since it is of the right type, since tryit implements the interface I am trying to cast it to.
 

Posts:370
Registered: 8/17/02
Re: java.lang.ClassCastException: plugin.tryit ?  
Jun 24, 2004 1:22 PM (reply 10 of 18)



 
this is such a friggen weird problem...
 

Posts:370
Registered: 8/17/02
Re: java.lang.ClassCastException: plugin.tryit ?  
Jun 24, 2004 1:30 PM (reply 11 of 18)



 
well guys I am gonna go if anyone can figure this out post this.. I have it on watch... I will be on later
 

Posts:6,750
Registered: 1/25/04
Re: java.lang.ClassCastException: plugin.tryit ?  
Jun 24, 2004 1:40 PM (reply 12 of 18)



 
OK how about this.
System.out.println(gh.getClass().getName());System.out.println(gh instanceof plugin.Interfaces.Interface);System.out.println(plugin.interfaces.Interface.isAssignableFrom(plugin.tryit.class));plugin.Interfaces.Interface xxx = (plugin.Interfaces.Interface) gh;
See what that gives you.
 

Posts:31,095
Registered: 4/30/99
Re: java.lang.ClassCastException: plugin.tryit ?  
Jun 24, 2004 2:13 PM (reply 13 of 18)



 
I'm guessing classloaders. Maybe the interface is not loaded by the same classloader as the class.

Um, rereading the original post I see that is indeed the case. Looks like that's your problem. Unfortunately I don't know how to fix it, but I'm sure there must be a way. My guess:
ClassLoader ldr = new java.net.URLClassLoader(url, this.getClass().getClassLoader());
PC²
 

Posts:6,750
Registered: 1/25/04
Re: java.lang.ClassCastException: plugin.tryit ?  
Jun 24, 2004 2:21 PM (reply 14 of 18)



 
Good eye, doctor.
 
This topic has 18 replies on 2 pages.    1 | 2 | Next »