package com;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
/**
* @author Administrator
* @function:动态加载任意目录下的架包
*
*/
public class DynamicLoadJar {
/**
* @author 2014-7-2 下午05:44:53
* @param jarSet 架包目录存放路径
*/
public static void loadJar(File[] jarSet) {
URLClassLoader cl = (URLClassLoader) Thread.currentThread()
.getContextClassLoader();
Class sysclass = URLClassLoader.class;
Method method = null;
try {
method = sysclass.getDeclaredMethod("addURL",
new Class[] { URL.class });
method.setAccessible(true);
for (File jar : jarSet) {
method.invoke(cl, jar.toURI().toURL());
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
/**
* @author Administrator
* @function:动态加载任意目录下的架包
*
*/
public class DynamicLoadJar {
/**
* @author 2014-7-2 下午05:44:53
* @param jarSet 架包目录存放路径
*/
public static void loadJar(File[] jarSet) {
URLClassLoader cl = (URLClassLoader) Thread.currentThread()
.getContextClassLoader();
Class sysclass = URLClassLoader.class;
Method method = null;
try {
method = sysclass.getDeclaredMethod("addURL",
new Class[] { URL.class });
method.setAccessible(true);
for (File jar : jarSet) {
method.invoke(cl, jar.toURI().toURL());
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}