Results 1 to 7 of 7
  1. #1
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default class loader problems!

    PHP 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
    im trying to load a class from a URL but when i try to load the class i get an error at compile time saying

    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

    PHP Code:
            public class appletClass extends Applet implements AppletStub{
    
                @Override public void init() {
    and then put the class loader code in the init() method and now im getting this error.


    so for some reason changing applet declaration from Applet applet to appletClass applet = new appletClass(); has made it not work.

    please help
    Teaching myself java so that i can eventually join the industry! Started in June 2010

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    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:

    Java Code:
    applet = (appletClass) (loader.newInstance());
    kind regards,

    Jos

  3. #3
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default

    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

  4. #4
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    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:

    Java Code:
    applet = (appletClass) (loader.newInstance());
    kind regards,

    Jos
    OK ill try that, thanks
    Teaching myself java so that i can eventually join the industry! Started in June 2010

  5. #5
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    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:

    Java Code:
    applet = (appletClass) (loader.newInstance());
    kind regards,

    Jos
    sorry still getting same error.
    Teaching myself java so that i can eventually join the industry! Started in June 2010

  6. #6
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default

    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

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Can you reduce your code to the smallest that will generate the error?
    Your small pieces from here and there don't show enough to work on.
    My quick test program compiles with no errors. There must be something else in your code that's the problem.

Similar Threads

  1. How Add new classess to the System Classs loader.
    By echarish in forum Advanced Java
    Replies: 0
    Last Post: 02-02-2010, 11:54 AM
  2. TCCL (Thread context class Loader) ???
    By OutOfMemory in forum Advanced Java
    Replies: 0
    Last Post: 01-07-2010, 01:38 PM
  3. Class loader
    By JavaJunkie in forum New To Java
    Replies: 1
    Last Post: 05-06-2009, 02:17 PM
  4. Smart Cache Loader 0.31
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 08-11-2007, 11:45 PM
  5. Smart Cache Loader 0.29
    By levent in forum Java Software
    Replies: 0
    Last Post: 07-26-2007, 09:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •