Results 1 to 7 of 7
Thread: class loader problems!
- 08-11-2010, 07:41 PM #1
class loader problems!
im trying to load a class from a URL but when i try to load the class i get an error at compile time sayingPHP Code:URLClassLoader classLoader2; String jarName = "test.jar"; String className = "test"; URL[] appletURL = { new URL(base + "/" + jarName) }; classLoader2 = new URLClassLoader(appletURL); Class loader = classLoader2.loadClass(className); applet = (appletClass) loader.newInstance(); //line 177
Exception in thread "main" java.lang.ClassCastException: loader cannot be cast to gui.Gui$appletClass
at line 177
i had this working before when i just declared applet from the Applet class,
(such as Applet applet) but then i decided to improve the code so i could make use of the .init() method etc so i made a class which extends applet
and then put the class loader code in the init() method and now im getting this error.PHP Code:public class appletClass extends Applet implements AppletStub{ @Override public void init() {
so for some reason changing applet declaration from Applet applet to appletClass applet = new appletClass(); has made it not work.
please helpTeaching myself java so that i can eventually join the industry! Started in June 2010
- 08-11-2010, 07:56 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
You don't have a class loader problem; it's just a simple Java syntax or semantic problem. A cast operator binds more tightly than an object dereference, so:
kind regards,Java Code:applet = (appletClass) (loader.newInstance());
Jos
- 08-11-2010, 08:01 PM #3
heres the code working before before modifications. ive also included the rest of method such as the catches etc which are present aswell in the modifications.
however i think its clumsy because i got an initApplet method and then applet.init() inside of it hence why i modified it
PHP Code:private void initApplet() { try { URLClassLoader classLoader2; String jarName = "test.jar"; String className = "test"; URL[] appletURL = { new URL(base + "/" + jarName) }; classLoader2 = new URLClassLoader(appletURL); Class loader = classLoader2.loadClass(className); applet = (Applet) loader.newInstance(); applet.setStub(this); applet.addMouseListener(this); applet.init(); applet.start(); } catch (InstantiationException ex) { Logger.getLogger(Gui.class.getName()).log(Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { Logger.getLogger(Gui.class.getName()).log(Level.SEVERE, null, ex); } catch (ClassNotFoundException ex) { Logger.getLogger(Gui.class.getName()).log(Level.SEVERE, null, ex); } catch (MalformedURLException ex) { Logger.getLogger(Gui.class.getName()).log(Level.SEVERE, null, ex); } }Teaching myself java so that i can eventually join the industry! Started in June 2010
- 08-11-2010, 08:04 PM #4
- 08-11-2010, 08:12 PM #5
- 08-11-2010, 09:00 PM #6
maybe it to do with the fact that previous i declared applet as
PHP Code:Applet applet;
and now its being declared as
PHP Code:appletClass applet = new appletClass();
ie because the applet is being set to a new object it doesnt allow it to load a class into it?Teaching myself java so that i can eventually join the industry! Started in June 2010
- 08-11-2010, 10:51 PM #7
Similar Threads
-
How Add new classess to the System Classs loader.
By echarish in forum Advanced JavaReplies: 0Last Post: 02-02-2010, 10:54 AM -
TCCL (Thread context class Loader) ???
By OutOfMemory in forum Advanced JavaReplies: 0Last Post: 01-07-2010, 12:38 PM -
Class loader
By JavaJunkie in forum New To JavaReplies: 1Last Post: 05-06-2009, 01:17 PM -
Smart Cache Loader 0.31
By JavaBean in forum Java SoftwareReplies: 0Last Post: 08-11-2007, 10:45 PM -
Smart Cache Loader 0.29
By levent in forum Java SoftwareReplies: 0Last Post: 07-26-2007, 08:09 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks