java动态加载jar包

package  com.te.classloader;


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  sun.misc.Launcher;

/**
 * 
@author  qking
 * 
@since  2007-8-06
 * 
@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);
            e.printStackTrace();
        }
        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();
    }

    
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);
            e.printStackTrace();
            
return   null ;
       }
    }

    
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);
            e.printStackTrace();
        }
    }

    
public   static   void  addURL2ExtClassLoader(URL url) {
        
try  {
            addURL.invoke(ext, 
new  Object[] { url });
        } 
catch  (Exception e) {
            
// throw new RootException(e);
            e.printStackTrace();
        }
    }

    
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);
            e.printStackTrace();
        }
    }

    
public   static   void  addExtClassPath(File dirOrJar) {
        
try  {
            addURL2ExtClassLoader(dirOrJar.toURL());
        } 
catch  (MalformedURLException e) {
            
// throw new RootException(e);
            e.printStackTrace();
        }
    }

}

获取系统ClassLoader,并对其加载的jar文件或者是类路径进行设置。

构造方法摘要
URLClassLoader(URL[] urls)
          使用默认的委托父 ClassLoader 为指定的 URL 构造一个新 URLClassLoader。
URLClassLoader(URL[] urls, ClassLoader parent)
          为给定的 URL 构造新 URLClassLoader。
URLClassLoader(URL[] urls, ClassLoader parent, URLStreamHandlerFactory factory)
          为指定的 URL、父类加载器和 URLStreamHandlerFactory 创建新 URLClassLoader。
 
方法摘要
protected  voidaddURL(URL url)
          将指定的 URL 添加到 URL 列表中,以便搜索类和资源。
protected  PackagedefinePackage(String name, Manifest man, URL url)
          用该 ClassLoader 中的名称定义一个新包。
protected  Class<?>findClass(String name)
          通过 URL 搜索路径查找并加载具有指定名称的类。
 URLfindResource(String name)
          在 URL 搜索路径中查找具有指定名称的资源。
 Enumeration<URL>findResources(String name)
          返回表示 URL 搜索路径中具有指定名称的所有资源的 URL 枚举。
protected  PermissionCollectiongetPermissions(CodeSource codesource)
          返回给定 codesource 对象的权限。
 URL[]getURLs()
          返回用于加载类和资源的 URL 搜索路径。
static URLClassLoadernewInstance(URL[] urls)
          为指定的 URL 和默认的父类加载器创建新 URLClassLoader 实例。
static URLClassLoadernewInstance(URL[] urls, ClassLoader parent)
          为指定的 URL 和父类加载器创建新 URLClassLoader 实例。

  URLClassLoader 的方法都是protected进行修饰的,在程序中无法进行设置,这个工具使用了反射机制,访问范围修饰符对反射机制无影响!

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值