Class Loader Tool class: Dynamically set Classpath, Get load class list, etc.

Source: Internet
Author: User

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);
}
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.