// why not load the righ class ?? please help me !
import java.io.*;
import java.lang.*;
import java.util.*;
import java.net.*;
import java.util.zip.*;
public class Loader extends ClassLoader{
private List JarList = null;
public Loader(){
JarList = new LinkedList();
}
public Loader(ClassLoader parent) {
super(parent);
JarList = new LinkedList();
}
public void addURL(ZipFile file){
if(file != null){
if(!JarList.contains(file)){
JarList.add(file);
}
}
}
public void removeURL(ZipFile file){
if(file != null){
if(JarList.contains(file)){
JarList.remove(file);
}
}
}
protected Class findClass(
String name) throws ClassNotFoundException {
try{
ZipEntry entry = getZipEntry(name);
InputStream stream = getInputStream(entry);
if(stream == null)
throw new ClassNotFoundException(name);
byte[] clsData = loadClassData(stream);
Class c = defineClass(name,clsData,0,clsData.length);
System.out.println("Class " + c);
if (c == null)
throw new ClassNotFoundException(name);
return c;
}catch(IOException e){
throw new ClassNotFoundException(name);
}
}
private ZipEntry getZipEntry(String clsname){
String entryname = clsname.replace('.','/');
entryname = entryname +".class";
return new ZipEntry(entryname);
}
private InputStream getInputStream(ZipEntry entry){
Iterator it = JarList.iterator();
InputStream stream = null;
while(it.hasNext()){
ZipFile file = (ZipFile)it.next();
try{
stream = file.getInputStream(entry);
if(stream != null)
break;
}catch(IOException e){
}
}
return stream;
}
private byte[] loadClassData(InputStream stream)throws IOException {
StringBuffer buffer = new StringBuffer();
while(true){
int byteValue = stream.read();
if(byteValue == -1) break;
buffer.append(byteValue);
}
return buffer.toString().getBytes();
}
public static void main(String args[]){
try{
ZipFile file = new ZipFile("e:\\j2ee.jar");
Loader loader = new Loader();
loader.addURL(file);
System.out.println(loader.loadClass("javax.ejb.EJBHome"));
}catch(Exception e){
e.printStackTrace();
}
}
}