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.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Development Class » Class Path 




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.