Search code examples
javaclassloaderdynamic-loading

Dynamic loading problem when program executes from a jar


I have an application which is extendable through java classes that conform to a given interface. If I run the program from the command line classes, I am able to instantiate the add-on classes using:

Class.forName("myAddon").newInstance();

However if I jar the application (setting the main class correctly), I get a class not found exception. Can anybody shed some light on what's going on here?

TIA, Adam


Solution

  • I have managed to get this working using a URLClassLoader, specifying the search path to be the current directory as follows:

    URLClassLoader cl = new URLClassLoader(new URL[] {new File(".").toURI().toURL()});
    

    Thanks for all your help,

    Adam