java ClassLoader的使用
public class Main { public static void main(String[] args) { URL[] urls = new URL[] {}; MyClassLoader classLoader = new MyClassLoader(urls, null); String path = "E:/WorkSpace/MapSDKLibrary/libs/commons-codec-1.6.jar"; try { classLoader.addJar(new File(path).toURI().toURL()); Class<?> clazz = classLoader.loadClass("org.apache.commons.codec.digest.DigestUtils"); Method md5Method = clazz.getDeclaredMethod("md5Hex",byte[].class); Object returnValue = md5Method.invoke(null, "hello world".getBytes("utf-8")); System.out.println("length="+returnValue.toString().length()+",result="+returnValue.toString()); classLoader.close(); /** ClassLoader cl = new URLClassLoader(new URL[]{new File(path).toURI().toURL()}); Class<?> clazz2 = classLoader.loadClass("org.apache.commons.codec.digest.DigestUtils"); Method md5Method2 = clazz.getDeclaredMethod("md5Hex",byte[].class); Object returnValue2 = md5Method.invoke(clazz2.newInstance(), "hello world".getBytes("utf-8")); System.out.println("length="+returnValue2.toString().length()+",result="+returnValue2.toString()); **/ } catch (Exception e) { e.printStackTrace(); } } static class MyClassLoader extends URLClassLoader { public MyClassLoader(URL[] urls) { super(urls); } public MyClassLoader(URL[] urls, ClassLoader parent) { super(urls, parent); } public void addJar(URL url) { this.addURL(url); } } }
java 使用ClassLoader动态加载jar,zip文件
原文:http://my.oschina.net/ososchina/blog/352008