本文共 1251 字,大约阅读时间需要 4 分钟。
用户可以通过 URLClassLoader 动态载入一个Class文件或者 jar 包
package com.humpic.framework.classloader; import java.io.File; import java.io.IOException; import java.net. * ; /** * 自定义 ClassLoader * * @since 2007-11-13 * @author Chen Guoqiang */ public class MyURLClassLoader extends URLClassLoader { public MyURLClassLoader() { super ( new URL[ 0 ], ClassLoader.getSystemClassLoader()); } public void addFiles(String[] files) { if (files == null || files.length <= 0 ) { return ; } for ( int i = 0 ; i < files.length; i ++ ) { if (files[i] != null && files[i].length() > 0 ) { addFile(files[i]); } } } public void addFiles(File[] files) { if (files == null || files.length <= 0 ) { return ; } for ( int i = 0 ; i < files.length; i ++ ) { addFile(files[i]); } } public void addURLs(URL[] urls) { if (urls == null || urls.length <= 0 ) { return ; } for ( int i = 0 ; i < urls.length; i ++ ) { addURL(urls[i]); } } public void addFile(String file) { try { addFile( new File(file).getCanonicalFile()); } catch (IOException e) { throw new RuntimeException(e); } } public void addFile(File file) { try { addURL(file.toURL()); } catch (MalformedURLException e) { throw new RuntimeException(e); } } public void addURL(URL url) { System.out.println(url); super .addURL(url); }} 转载地址:http://vznti.baihongyu.com/