@Test
public void testLoaderLog4j() {
try {
URL[] urls= {
new File("D:/ws/PA18Shopauto1.42.0/dist/weblogic/pa18shopauto/APP-INF/lib/log4j-1.2.15.jar").toURI().toURL(),
new File("D:/ws/PA18Shopauto1.42.0/dist/weblogic/pa18shopauto/APP-INF/lib/commons-logging.jar").toURI().toURL()};
MyClassLoader loader = new MyClassLoader(urls);
Class clazz=loader.loadClass("org.apache.commons.logging.impl.LogFactoryImpl");
/**
* if set this contextLoader, that means
*/
Thread.currentThread().setContextClassLoader(loader);
Class[] parameterTypes = new Class[1];
parameterTypes[0] = String[].class;
//the getMethod only returns the public method
//Method m= clazz.getMethod("newInstance", parameterTypes);
Method[] mmMethod = clazz.getDeclaredMethods();
for(int i=0;i<mmMethod.length;i++) {
System.out.println(mmMethod[i].getName());
}
Method m = clazz.getDeclaredMethod("newInstance", String.class);
m.setAccessible(true);
m.invoke(clazz.newInstance(),clazz.getCanonicalName());
System.out.println(clazz.getCanonicalName());
}
catch(Exception e) {
e.printStackTrace();
}
}
public class MyClassLoader extends URLClassLoader{
public MyClassLoader(URL[] urls) {
super(urls);
}
public void addURL(URL[] urls) {
for(int i=0;i<urls.length;i++) {
addURL(urls[i]);
}
}
public void addURL(URL url) {
super.addURL(url);
}
}