• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Classloader and casting, how to?

 
daniele licitra
Ranch Hand
Posts: 81
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i've implemented the loading procedure has described in http://www.coderanch.com/t/383279/java/java/dynamically-loading-class-file-jar
This procedure works.

I've an abstract class AbstractModule that implement the interface ModuleInterface.
Now, I build some different class, Module1, Module2, ecc that extends AbstractModule.

I use this function to load and instantiate the class:


In the core of the program i use

where modulo is of type AbstractModule.
The problem is in the row signed by the arrow or in the calling of the loader over there, i recive the exception:

SEVERE: java.lang.ClassCastException: com.bss.framework.bssmoduloutenti.ModuloGrafico cannot be cast to com.bss.framework.astratte.AbstractModule
at com.bss.framework.utils.BssLoader.caricaModulo(BssLoader.java:92) <-- this is the arrowed key
at com.bss.framework.servizi.SessioneUtente.setModuloCorrente(SessioneUtente.java:178) <--- this is the calling

How can I solve it? There is something wrong?
Thanks
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15452
42
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, does com.bss.framework.bssmoduloutenti.ModuloGrafico indeed extend com.bss.framework.astratte.AbstractModule?

Furthermore, the problem might be that you load the ModuloGrafico class using another classloader than what AbstractModule was loaded with. I don't know the exact details, but as far as I know classloaders are separated in such a way that a class loaded with one classloader is in a separate space than classes loaded with a different classloader.

It's just a guess and I'm not sure if it will work, but I think it might work if you initialize your URLClassLoader differently, setting the classloader of AbstraceModule as the parent:

 
daniele licitra
Ranch Hand
Posts: 81
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank for replying.


i've modified the loader function according to yout reply:


i send the Classloader c from the caller and it works! Thanks a lot
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic