Search code examples
javaclassloader

How to set classloader for XMLDecoder?


I have a small program which uses plugins. I am using Java's built in XMLDecoder/Encoder system to save data files for this program. The snag I'm having is that when I go to load these files using the XMLDecoder, I get a message stating, that the class is not in the classpath.

Essentially what I have is a classloader for each plugin and during normal execution, those class loaders create new instances of those classes. So my question is, how can I easily supply these urls containing the classpaths to the XMLDecoder class loader?

Thank you for the help,

Jec


Solution

  • In some cases, you can just pass the ClassLoader of the calling class when you instantiate XMLDecoder.

    XMLDecoder decoder = new XMLDecoder(in, null, null, this.getClass().getClassLoader());