类加载器工具类:动态设置classpath,获得加载类列…
2008-02-23 09:18:50来源:互联网 阅读 ()
下面的一个小工具类提供了对系统类加载器和扩展类加载器的动态控制能力.
可以在程序中加入classpath,当然也可以获得类加载器加载的类列表,相信
Java的动态能力!
package org.rut.core;
import java.io.File;
import java.io.PrintStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.List;
import org.rut.RootException;
import sun.misc.Launcher;
/**
* @author treeroot
* @since 2006-2-12
* @version 1.0
*/
public class ClassLoaderUtil {
private static Field classes;
private static Method addURL;
static {
try {
classes = ClassLoader.class.getDeclaredField("classes");
addURL = URLClassLoader.class.getDeclaredMethod("addURL",
new Class[] { URL.class });
} catch (Exception e) {
//won't happen ,but remain it
throw new RootException(e);
}
classes.setAccessible(true);
addURL.setAccessible(true);
}
private static URLClassLoader system = (URLClassLoader) getSystemClassLoader();
private static URLClassLoader ext = (URLClassLoader) getExtClassLoader();
public static ClassLoader getSystemClassLoader() {
return ClassLoader.getSystemClassLoader();
}
public static ClassLoader getExtClassLoader() {
return getSystemClassLoader().getParent();
}
/**
* 获得加载的类
*
* @return
*/
public static List getClassesLoadedBySystemClassLoader() {
return getClassesLoadedByClassLoader(getSystemClassLoader());
}
public static List getClassesLoadedByExtClassLoader() {
return getClassesLoadedByClassLoader(getExtClassLoader());
}
public static List getClassesLoadedByClassLoader(ClassLoader cl) {
try {
return (List) classes.get(cl);
} catch (Exception e) {
throw new RootException(e);
}
}
public static URL[] getBootstrapURLs() {
return Launcher.getBootstrapClassPath().getURLs();
}
public static URL[] getSystemURLs() {
return system.getURLs();
}
public static URL[] getExtURLs() {
return ext.getURLs();
}
private static void list(PrintStream ps, URL[] classPath) {
for (int i = 0; i < classPath.length; i ) {
ps.println(classPath[i]);
}
}
public static void listBootstrapClassPath() {
listBootstrapClassPath(System.out);
}
public static void listBootstrapClassPath(PrintStream ps) {
ps.println("BootstrapClassPath:");
list(ps, getBootstrapClassPath());
}
public static void listSystemClassPath() {
listSystemClassPath(System.out);
}
public static void listSystemClassPath(PrintStream ps) {
ps.println("SystemClassPath:");
list(ps, getSystemClassPath());
}
public static void listExtClassPath() {
listExtClassPath(System.out);
}
public static void listExtClassPath(PrintStream ps) {
ps.println("ExtClassPath:");
list(ps, getExtClassPath());
}
public static URL[] getBootstrapClassPath() {
return getBootstrapURLs();
}
public static URL[] getSystemClassPath() {
return getSystemURLs();
}
public static URL[] getExtClassPath() {
return getExtURLs();
}
public static void addURL2SystemClassLoader(URL url) {
try {
addURL.invoke(system, new Object[] { url });
} catch (Exception e) {
throw new RootException(e);
}
}
public static void addURL2ExtClassLoader(URL url) {
try {
addURL.invoke(ext, new Object[] { url });
} catch (Exception e) {
throw new RootException(e);
}
}
public static void addClassPath(String path) {
addClassPath(new File(path));
}
public static void addExtClassPath(String path) {
addExtClassPath(new File(path));
}
public static void addClassPath(File dirOrJar) {
try {
addURL2SystemClassLoader(dirOrJar.toURL());
} catch (MalformedURLException e) {
throw new RootException(e);
}
}
public static void addExtClassPath(File dirOrJar) {
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:各种排序算法java实现
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash