import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class FileClassLoader extends ClassLoader
{
public static final String drive = "F:\\photomanage\\hard\\bin\\zhenghe\\";
public static final String fileType = ".class";
public FileClassLoader() {
super();
}
public FileClassLoader(ClassLoader arg0) {
super(arg0);
}
public Class findClass(String name)
{
byte[] data = loadClassData(name);
return defineClass(name, data, 0, data.length);
}
public byte[] loadClassData(String name)
{
FileInputStream fis = null;
byte[] data = null;
try
{
fis = new FileInputStream(new File(drive + name + fileType));
System.out.println(drive + name + fileType);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int ch = 0;
while ((ch = fis.read()) != -1)
{
baos.write(ch);
}
data = baos.toByteArray();
} catch (IOException e)
{
e.printStackTrace();
}
return data;
}
public static void main(String[] args) throws Exception
{
FileClassLoader loader = new FileClassLoader();
Class objClass = (Class) loader.loadClass("Bhui", true).newInstance();
Object obj = objClass.newInstance();
// System.out.println(objClass.getName());
// System.out.println(objClass.getClassLoader());
}
} |
|