This is an excerpt from an interesting artice i found inside a recent Java Developers Journal( Aug 2003 Vol:9 Issue:8) magazine.
:ehh: Ok sounds easy enough but i have a few questions. Am i doing this right? :D How can i scope the size of the byte array to the number of bytes within the file? Once the class is returned from the classloader how can i execute it? More questions to come......... :lol:Quote:
The loadClass(String classname) method invokes the
findClass(String classname) method. Custom ClassLoaders
shoud override this method to provide a custom way of
locating and loading a Java class file.
Code:import java.io.*;
public class Loader extends ClassLoader{
private byte[] b;
public Class findClass(String c) throws ClassNotFoundException{
try{
Scanner s = new Scanner(c);
this.b = s.getByteCode();
}catch(IOException e){;}
return b.getClass();
}
}
Code:
public class Scanner{
private File res;
public Scanner(String r){
res = new File(r);
}
public byte[] getByteCode() throws IOException{
BufferedInputStream buff;
buff = new BufferedInputStream(new FileInputStream(res));
byte[] b = new byte[5000];
long bytesread = 0;
while(true){
bytesread = buff.read(b);
if(bytesread < 0) break;
}
return b;
}
}
