Self ClassLoader : Class Path « Development Class « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Development Class » Class PathScreenshots 
Self ClassLoader
     
/*
 * @(#)SelfClassLoader.java 2010-4-18
 
 * Copyright 5jxiang . All rights reserved.
 */
//package com.jz.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.WritableByteChannel;

/**
 * <p>
 * <a href="SelfClassLoader.java.html"><i>View Source</i></a>
 * </p>
 *
 @author 5jxiang
 @version $Id$
 */
public class SelfClassLoader extends ClassLoader {
  protected Class findClass(String namethrows ClassNotFoundException {
      byte[] bytes = loadClassBytes(name);
      Class theClass = defineClass(name, bytes, 0, bytes.length);
      if (theClass == null)
      throw new ClassFormatError();
      return theClass;
   }
   private byte[] loadClassBytes(String classNamethrows
        ClassNotFoundException {
   try {
     String classFile = getClassFile(className);
     FileInputStream fis = new FileInputStream(classFile);
     FileChannel fileC = fis.getChannel();
     ByteArrayOutputStream baos = new
     ByteArrayOutputStream();
     WritableByteChannel outC = Channels.newChannel(baos);
     ByteBuffer buffer = ByteBuffer.allocate(1024);
     while (true) {
          int i = fileC.read(buffer);
          if (i == || i == -1) {
           break;
           }
           buffer.flip();
           outC.write(buffer);
           buffer.clear();
         }
         fis.close();
         return baos.toByteArray();
       catch (IOException fnfe) {
         throw new ClassNotFoundException(className);
       }
   }
   
  private String getClassFile(String name){
       StringBuffer sb = new StringBuffer(SelfClassLoader.class.getResource("/").getPath());
       name = name.replace('.', File.separator.charAt(0)) ".class";
       sb.append(File.separator + name);
       return sb.toString();
     }
}

   
    
    
    
    
  
Related examples in the same category
1.getClass().getResourceAsStream
2.Try adding one or more item(s) to class path
3.Load resource file: absolute from the classpath
4.Load resource file: relative to the class location
5.relative document loading in classpath
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.