2013-10-24 3 views
0

런타임에 내 프로그램 클래스 경로에 Jar를 동적으로 추가하려고 시도하고 있습니다. THIS 메소드를 사용하여 발견했습니다. 많은 사람들에게 효과적 이었기 때문에 발견되었습니다. addPlugin()을 사용하는 경우 NoSuchMethodException을 던집니다 (아래 코드에 설명되어 있음).NoSuchMethodException 클래스 경로에 동적으로 추가 할 때

누군가가이 문제를 해결하기 위해 내가 누락 된 부분을 알려주시겠습니까? 나는 이것에 너무 친숙하지 않다. 그리고 나는 그것을 전에 보려고 노력했다.

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"); 
     } 
    } 
} 

사용법 :

PluginLoader.addPlugin(new File("../path/to/jar.jar")); 
Constructor<?> cs = ClassLoader.getSystemClassLoader().loadClass("my.main.class.Main").getConstructor(String.class); 

답변

0

변경 :

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

에 :

Method m = plClass.getDeclaredMethod("addURL", _PARAMS); 
관련 문제