- import java.util.Date;
- public class ClassLoaderTest {
- /**
- * @param args
- * @throws ClassNotFoundException
- * @throws IllegalAccessException
- * @throws InstantiationException
- */
- public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
- // TODO Auto-generated method stub
- Class clazz=new MyClassLoader("xiaodai").loadClass("ClassLoaderDai");//问题为什么这里不调用findClass()函数呢?而调用
- Date date=(Date)clazz.newInstance(); //
- loaderClass ()方法呢?调用父类的
- loaderClass 方法
- 如
- System.out.println(date); //何将”ClassLoaderDai“传给子类的呢?子类有如何用的
- } //呢?
- }
复制代码- import java.io.ByteArrayOutputStream;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- public class MyClassLoader extends ClassLoader{
- /**
- * @param args
- * @throws IOException
- */
- public static void main(String[] args) throws IOException {
- // TODO Auto-generated method stub
- String readFile=args[0];
- String writefiles=args[1];
- String file=readFile.substring(readFile.lastIndexOf("\\")+1);
- String writefile=writefiles+"\\"+file;
- InputStream is=new FileInputStream(readFile);
- OutputStream os=new FileOutputStream(writefile);
- cyper(is,os);
- is.close();
- os.close();
- }
- public static void cyper(InputStream is,OutputStream os) throws IOException{
- int ips=-1;
- while((ips=is.read())!=-1){
- os.write(ips^0xff);
-
- }
-
- }
- @Override
- protected java.lang.Class<?> findClass(String name) throws ClassNotFoundException {
- String readFile=classpaty+"\\"+name+".class";
- try {
- InputStream is=new FileInputStream(readFile);
- ByteArrayOutputStream os=new ByteArrayOutputStream();
- cyper(is,os);
- is.close();
- byte[] b=os.toByteArray();
- return defineClass(b, 0, b.length);
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return null;
-
- }
-
- private String classpaty;
- MyClassLoader(){
-
- }
- MyClassLoader(String classpaty){
- this.classpaty=classpaty;
- }
- }
复制代码- import java.util.Date;
- public class ClassLoaderDai extends Date {
- public String toString(){
- return "hello java";
- }
- }
复制代码 |