This discussion is archived
1 2 3 Previous Next 30 Replies Latest reply: Sep 25, 2012 11:14 AM by 824461 RSS

ClassCastException in Custom class loader

824461 Newbie
Currently Being Moderated
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();

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

Please help.
  • 1. Re: ClassCastException in Custom class loader
    DrClap Expert
    Currently Being Moderated
    That's most likely because the system class-loader (the one which loads MyObjectSuperClass) isn't the parent of your custom class-loader.
  • 2. Re: ClassCastException in Custom class loader
    824461 Newbie
    Currently Being Moderated
    But when my subclass MyObject is loaded by custom loader then the same custom loader will load all its superclass as well correct?
    In that case class loader for both MyObject and MyObjectSuperClass are same.

    Any ways I have set the parent class loader as well in customer class loader


    public ClassLoaderDemo(String myPackage, String codeBase) {
    // NEW
         super(ClassLoaderDemo.class.getClassLoader());
    this.myPackage = myPackage;
    this.codeBase = codeBase;
    }
  • 3. Re: ClassCastException in Custom class loader
    824461 Newbie
    Currently Being Moderated
    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();

    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

    Please help.
  • 4. Re: ClassCastException in Custom class loader
    DrClap Expert
    Currently Being Moderated
    821458 wrote:
    But when my subclass MyObject is loaded by custom loader then the same custom loader will load all its superclass as well correct?
    In that case class loader for both MyObject and MyObjectSuperClass are same.
    No. Your custom class loader should allow its parent to load classes if it can. So that means that making the system class loader be your class loader's parent means that your class loader should only be used to load classes which aren't in the classpath.

    And when your code does casting like that, the compiler generates code which assumes that the class being cast to will be loaded by the system class loader. That means that when your class loader is asked to load such a class, it should ask its parent (the system class loader) to load it.
  • 5. Re: ClassCastException in Custom class loader
    824461 Newbie
    Currently Being Moderated
    1 then this should be true for MyObject as well, that should also be loaded by system class loader.

    2 MyObject and MyObjectSuperClass are not in classpath
    3 and also I have set the parent class loader as system class loader even then I am getting class cast exception
  • 6. Re: ClassCastException in Custom class loader
    DrClap Expert
    Currently Being Moderated
    821458 wrote:
    2 MyObject and MyObjectSuperClass are not in classpath
    And therefore (as I already pointed out) you can't cast anything to MyObjectSuperClass.
  • 7. Re: ClassCastException in Custom class loader
    824461 Newbie
    Currently Being Moderated
    DrClap wrote:
    821458 wrote:
    But when my subclass MyObject is loaded by custom loader then the same custom loader will load all its superclass as well correct?
    In that case class loader for both MyObject and MyObjectSuperClass are same.
    No. Your custom class loader should allow its parent to load classes if it can. So that means that making the system class loader be your class loader's parent means that your class loader should only be used to load classes which aren't in the classpath.
    But as you said if they are not in the classpath custom class loader will load it. that mean in this case since MyObject and MyObjectSuperClass are not in classpath will be loaded by custom class loader and since they are loaded from the same class loader casting should be possible.

    I am little confused here. Please help me understand here

    When. a custom class loader loads a class it is the same custom class loder which loads its parent class provided they are not in classpath or loaded by bootstrap loader.

    This statement is correct?
  • 8. Re: ClassCastException in Custom class loader
    824461 Newbie
    Currently Being Moderated
    One more thing you said that class cast exception is because I might not have set parent class loader but I have that too. Why is that exception then.
  • 9. Re: ClassCastException in Custom class loader
    DrClap Expert
    Currently Being Moderated
    As I already said:

    Coding a cast to a class in your Java code means that the class was in the classpath at compile time. The compiler generates code which assumes that the class will likewise be in the classpath at run time.

    You said the class (the one which you cast to) wasn't in the classpath at run time. Hence the error.

    The usual way that people handle this situation is to have a superclass, or more commonly a super interface, in the classpath at run time. Then you can load customized implementations of this interface via your specialized class loader and cast them to a variable of that interface type.
  • 10. Re: ClassCastException in Custom class loader
    824461 Newbie
    Currently Being Moderated
    Can you tell me how I would have done differently so that this casting is possible.
    can you please give me a small code snippet.

    Thanks a lot
  • 11. Re: ClassCastException in Custom class loader
    DrClap Expert
    Currently Being Moderated
    821458 wrote:
    Can you tell me how I would have done differently so that this casting is possible.
    I just did exactly that in my last reply. If you aren't going to read the answers to the questions you ask, why do you bother to ask them? And for that matter, why do I bother to answer them?
  • 12. Re: ClassCastException in Custom class loader
    Tolls Journeyer
    Currently Being Moderated
    DrClap wrote:
    And for that matter, why do I bother to answer them?
    Because, like the rest of us, you suffer from some sort of strange compulsion?
    :)
  • 13. Re: ClassCastException in Custom class loader
    824461 Newbie
    Currently Being Moderated
    >
    The usual way that people handle this situation is to have a superclass, or more commonly a super interface, in the classpath at run time. Then you can load customized implementations of this interface via your specialized class loader and cast them to a variable of that interface type.
    I have pasted the code and I have done the same thing.
    I have used the super class "MySuperObjectClass".

    MyObjectSuperClass o1 = (MyObjectSuperClass)clas.newInstance();

    In this case I am getting classcastexcpetion.

    In current implementation of custom class loader all the super class and interface of MyObject are being loaded by the custom class loader only. But when I do

    System.out.println(MyObjectSuperClass.class.getClassLoader());

    it says its the system class loader. Why, when this class has been loaded by my custom class loader.
  • 14. Re: ClassCastException in Custom class loader
    824461 Newbie
    Currently Being Moderated
    Please answer
1 2 3 Previous Next

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points