Results 1 to 3 of 3
  1. #1
    lodha13 is offline Member
    Join Date
    Sep 2012
    Posts
    1
    Rep Power
    0

    Default ClassCastException in Custom class loader

    1.
    I have created a custom class loder.

    ClassLoaderDemo ccl = new ClassLoaderDemo(); //assume it is working fine
    Class clas = ccl.loadClass( "com.MyObject" );

    com.MyObjectSuperClass o1 = (com.MyObjectSuperClass)clas.newInstance(); //MyObjectSuperClass is super class of MyObject

    Now I want to cast the loaded object to MyObjectSuperClass which is super class if MyObject. But I am getting the following exception

    Exception in thread "main" java.lang.ClassCastException: com.MyObject cannot be cast to com.MyObjectSuperClass

    2.
    Object o = ccl.loadClass( "com.MyObject" ).newInstance();
    SOP(o.getClass().getClassLoader()); //This statement returns my custom class loader as excpected.

    but

    SOP(MyObject.class.getClassLoader()); //This return system class loader Why? when this class has already been loaded by my custom class loader.
    Please help.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,967
    Rep Power
    19

    Default Re: ClassCastException in Custom class loader

    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: ClassCastException in Custom class loader

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Make JVM use My Own Class Loader
    By rajyshubeita@gmail.com in forum Advanced Java
    Replies: 1
    Last Post: 11-18-2011, 09:41 PM
  2. which class loader cexecutes first
    By katturv in forum New To Java
    Replies: 1
    Last Post: 01-10-2011, 06:30 AM
  3. Replies: 6
    Last Post: 09-20-2010, 07:46 AM
  4. class loader problems!
    By alacn in forum New To Java
    Replies: 6
    Last Post: 08-11-2010, 10:51 PM
  5. Class loader
    By JavaJunkie in forum New To Java
    Replies: 1
    Last Post: 05-06-2009, 01:17 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
  •