0

I'm trying to dynamically add Jar's to my programs classpath at run time using THIS method i found since it seems like it worked for a lot of people. When using addPlugin() it throws a NoSuchMethodException (commented in the code below).

Can someone please tell me what I'm missing in order to get this working? I'm not too familiar with this and I've tried looking it up before.

public final class PluginLoader {
    private static final Class[] _PARAMS = new Class[] {URL.class};

    public static void addPlugin(File plugin) throws PluginException {
        URLClassLoader plLoader = (URLClassLoader)ClassLoader.getSystemClassLoader();
        Class plClass = URLClassLoader.class;
        try {
                Method m = plClass.getDeclaredMethod("addPlugin", _PARAMS); //ERROR HERE
                m.setAccessible(true);
                m.invoke(plLoader, new Object[] {plugin.toURI().toURL()});
        } catch (Exception ex) {
                ex.printStackTrace();
                throw new PluginException("ERROR: Could not add plugin '" + plugin.getName() + "' to System ClassLoader");
        }
    }
}

Usage:

PluginLoader.addPlugin(new File("../path/to/jar.jar"));
Constructor<?> cs = ClassLoader.getSystemClassLoader().loadClass("my.main.class.Main").getConstructor(String.class);
Community
  • 1
  • 1
Kyle Colantonio
  • 454
  • 1
  • 3
  • 21

1 Answers1

0

Change:

Method m = plClass.getDeclaredMethod("addPlugin", _PARAMS);

to:

Method m = plClass.getDeclaredMethod("addURL", _PARAMS);
Josh M
  • 11,611
  • 7
  • 39
  • 49