首页 > 代码库 > java 使用ClassLoader动态加载jar,zip文件
java 使用ClassLoader动态加载jar,zip文件
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文件
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。