JAVA在应用程序中加入类或者JAR包

package net.zhangyz.common;

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.apache.log4j.Logger;

import sun.misc.Launcher;

public class ClassLoaderUtil {
    private static Field classes;
    private static Logger logger=Logger.getLogger(ClassLoaderUtil.class);

    private static Method addURL;
    static {
        try {
             classes = ClassLoader.class.getDeclaredField("classes");
             addURL = URLClassLoader.class.getDeclaredMethod("addURL",new Class[] { URL.class });
         } catch (Exception e) {
             logger.error("init the \"classes\" field of ClassLoader or the \"addURL\" method of URLClassLoader is error", 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();
     }

    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) {
        	 logger.error("the \"classes\" field's value of ClassLoader", e);
            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) {
            logger.error("add jar or classes to SystemClassLoader", e);
         }
     }

    public static void addURL2ExtClassLoader(URL url) {
        try {
             addURL.invoke(ext, new Object[] { url });
         } catch (Exception e) {
        	 logger.error("add jar or classes to ExtClassLoader", 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) {
        	 logger.error("add jar or classes to SystemClassLoader", e);
         }
     }

    public static void addExtClassPath(File dirOrJar) {
        try {
             addURL2ExtClassLoader(dirOrJar.toURL());
         } catch (MalformedURLException e) {
        	 logger.error("add jar or classes to ExtClassLoader", e);
         }
     }

    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
    	// location of the class is "E:/bin/net/zhangyz/common"
		addClassPath("E:/bin");
		logger.info(Class.forName("net.zhangyz.common.Test").newInstance());
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值