The following gadget class provides dynamic control over the System class loader and the extended class loader.
You can add classpath to your program, but you can also get a list of classes loaded by the ClassLoader, and believe
The dynamic capabilities of 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 ();
}
/**
* Get the Loaded class
*
* @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) {
try {
Addurl2extclassloader (Dirorjar.tourl ());
catch (Malformedurlexception e) {
throw new Rootexception (e);
}
}
}