public class Test {
public static void main(String[] args) throws MalformedURLException, ClassNotFoundException,
InstantiationException, IllegalAccessException,
IllegalArgumentException, InvocationTargetException,
NoSuchMethodException, SecurityException {
URL urls[] = new URL[1];
urls[0] = new URL("file:e:/program/java/Api.jar");
URLClassLoader loader = new URLClassLoader(urls);
//如果用于WEB应用,则需要使用以下构造方法
//URLClassLoader loader = new URLClassLoader(urls, Thread.currentThread().getContextClassLoader());
Class<?> api = loader.loadClass("com.wogu.Api");
Constructor<?> constructors[] = api.getDeclaredConstructors();
Object obj = constructors[0].newInstance();
Method method = api.getMethod("test");
method.invoke(obj);
}
}
【java】动态加载类-URLClassLoader
最新推荐文章于 2023-10-15 22:41:08 发布