zoukankan      html  css  js  c++  java
  • 自定义类加载器classLoader

    package cn.bdqn.userconsumer;

    import java.io.*;

    /**
    * 自定义classLoader 类加载器
    */
    public class HelloClassLoad extends ClassLoader {

    /**
    * 自定义类加载器
    * 继承classLoader 重写findClass()方法
    * @param name
    * @return
    * @throws ClassNotFoundException
    */
    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
    File file = new File("F:\test",name.replace(".","\").concat(".class"));
    try {
    FileInputStream inputStream = new FileInputStream(file);
    //FileOutputStream fileoutput = new FileOutputStream(new File("F:/test",name.replace(".","/").concat(".class")));
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    int by=0;
    while((by = inputStream.read()) !=0){
    outputStream.write(by);
    }

    byte[] bytes = outputStream.toByteArray();
    outputStream.close();

    defineClass(name,bytes,0,bytes.length);
    } catch (Exception e) {
    e.printStackTrace();
    }

    return super.findClass(name);
    }


    public static void main(String[] args) {
    try {
    //使用自定义 classLoader
    ClassLoader classLoader = new HelloClassLoad();
    //加载指定目录下的类文件
    Class<?> aClass = classLoader.loadClass("cn.bdqn.userconsumer.HelloJol");
    HelloJol helloJol = (HelloJol) aClass.newInstance();
    helloJol.Str();
    System.out.println(classLoader.getClass().getClassLoader());
    System.out.println(classLoader.getParent());
    } catch (Exception e) {
    e.printStackTrace();
    }
    }







    }
  • 相关阅读:
    POJ 2342.Anniversary party-树形dp
    Codeforces Round #363 (Div. 2) A、B、C
    Codeforces Beta Round #17 D.Notepad 指数循环节
    hdu 5920 Wool 思路
    hdu 5719 Arrange 贪心
    hdu 5718 Oracle 高精度
    hiho #1332 : 简单计算器 栈+递归
    UESTC 1074 秋实大哥搞算数 栈模拟
    cdoj 1329 卿学姐与魔法 优先队列
    cdoj 1324 卿学姐与公主 线段树裸题
  • 原文地址:https://www.cnblogs.com/lixiangang/p/14820177.html
Copyright © 2011-2022 走看看