⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 liyaclassloader.java

📁 采用JAVA开发
💻 JAVA
字号:
package com.gctech.sms.util;

import java.io.*;
import java.net.*;
import java.util.StringTokenizer;
import java.nio.channels.*;
import java.nio.*;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: gctech</p>
 * @author 王红宝
 * @version $Id: LiyaClassLoader.java,v 1.1.1.1 2004/04/21 09:30:41 wanghb Exp $
 */



public class LiyaClassLoader extends ClassLoader {
  //private String baseDir;
  //private static final Logger LOG =Logger.getLogger(RayClassLoader.class);

  public LiyaClassLoader (ClassLoader parent) {
    super(parent);
  }

  public Class findClass(String name) throws ClassNotFoundException {
    //LOG.debug("findClass " + name);
    byte[] bytes = loadClassBytes(name);

    Class theClass = defineClass(name, bytes, 0, bytes.length);
    if (theClass == null)
      throw new ClassFormatError();

    return theClass;
  }

  private byte[] loadClassBytes(String className) throws ClassNotFoundException {
    try {
      File classFile = getClassFile(className);

      FileInputStream fis = new FileInputStream(classFile);
      FileChannel fileC = fis.getChannel();
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      WritableByteChannel outC = Channels.newChannel(baos);
      ByteBuffer buffer = ByteBuffer.allocateDirect(1024);

      while (true) {
        int i = fileC.read(buffer);
        if (i == 0 || i == -1) {
          break;
        }
        buffer.flip();
        outC.write(buffer);
        buffer.clear();
      }
      fis.close();

      return baos.toByteArray();
    } catch (IOException fnfe) {
      throw new ClassNotFoundException(className);
    }
  }

  private File getClassFile(String className) throws ClassNotFoundException{
    /**取环境变量*/
    String classPath = System.getProperty("java.class.path");
    List classRepository = new ArrayList();
    /**取得该路径下的所有文件夹 */
    if ( (classPath != null) && ! (classPath.equals(""))) {
      StringTokenizer tokenizer = new StringTokenizer(classPath,
          File.pathSeparator);
      while (tokenizer.hasMoreTokens()) {
        classRepository.add(tokenizer.nextToken());
      }
    }
    String classFileName = null;
    for ( Iterator dirs = classRepository.iterator(); dirs.hasNext(); ){
      String dir = (String) dirs.next();
      //replace '.' in the class name with File.separatorChar & append .class to the name
      classFileName = className.replace('.', File.separatorChar);
      classFileName += ".class";
      System.out.println("classFileName" + classFileName);
      System.out.println("className" + className);
      File file = new File(dir + File.separatorChar + classFileName);
      if (file.exists()) {
        return file;
      }
    }
    throw new ClassNotFoundException("can't find the class:"+classFileName);
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -