黑马程序员技术交流社区

标题: 类加载问题,这道题我搞了10个小时,都没搞定问题在哪 [打印本页]

作者: 高鑫    时间: 2012-8-10 20:52
标题: 类加载问题,这道题我搞了10个小时,都没搞定问题在哪
本帖最后由 高鑫 于 2012-8-10 20:54 编辑
  1. //存储文件名ClassLoaderAttachment.java
  2. package df;


  3. import java.util.*;
  4.    
  5. class ClassLoaderAttachment extends Date{
  6.         public String toString(){
  7.            return "Hello, world!";
  8.         }
  9. }
复制代码
  1. //存储文件名MyClassLoader.java
  2. package df;
  3. import java.io.*;

  4. class MyClassLoader extends ClassLoader{
  5.         public static void main(String args[])throws Exception{
  6.             String srcPath=args[0];
  7.             String destDir=args[1];
  8.             FileInputStream fis=new FileInputStream(srcPath);
  9.             String destFileName=srcPath.substring(srcPath.lastIndexOf('\\')+1);
  10.             String destPath=destDir+"\\"+destFileName;
  11.             FileOutputStream fos=new FileOutputStream(destPath);
  12.             cypher(fis,fos);
  13.             fis.close();
  14.             fos.close();
  15.             }
  16.             private static void cypher(InputStream ips,OutputStream ops)throws Exception{
  17.                     int b=-1;
  18.                     while((b=ips.read())!=-1){
  19.                             ops.write(b^0xff);        
  20.                     }
  21.             }
  22.             private String classDir;
  23.             @Override
  24.      
  25.             protected Class<?>findClass(String name) throws ClassNotFoundException{
  26.                     String classFileName=classDir+"\\"+name+".class";
  27.                     try{
  28.                            
  29.                             FileInputStream fis=new FileInputStream(classFileName);
  30.                             ByteArrayOutputStream bos=new ByteArrayOutputStream();
  31.                             cypher(fis,bos);
  32.                             fis.close();
  33.                            
  34.                             System.out.println("aaaa");
  35.                            
  36.                             byte[] bytes=bos.toByteArray();
  37.                             return defineClass(bytes,0,bytes.length);
  38.                     }
  39.                     catch(Exception e){
  40.                             e.printStackTrace();
  41.                     }
  42.                     return null;
  43.             }
  44.             public MyClassLoader(){
  45.                     
  46.             }
  47.             public MyClassLoader(String classDir){
  48.                     this.classDir=classDir;        
  49.             }
  50. }

复制代码
  1. //文件名ClassLoaderTest.java
  2. package df;
  3. import java.util.Date;

  4. class ClassLoaderTest{
  5.         public static void main(String args[]) throws ClassNotFoundException, InstantiationException, IllegalAccessException{
  6.              Class<?> clazz=new MyClassLoader("itcastlib").loadClass("ClassLoaderAttachment");
  7.         Date d1=(Date) clazz.newInstance();
  8.              System.out.println(d1);
  9.                  
  10.         }
  11. }
复制代码
这个是老师的例题,第一个程序存储文件名ClassLoaderAttachment.java,第二个MyClassLoader.java第三个文件名ClassLoaderTest.java, 这个是老师的例题,可我按顺序运行,吧加密的ClassLoaderAttachment.class放到指定的目录下后,并删除原来的ClassLoaderAttachment.class,运行ClassLoaderTest就是报错, java.lang.IllegalAccessException: Class df.ClassLoaderTest can not access a member of class df.ClassLoaderAttachment with modifiers ""
请问哪里出了问题??
作者: 高鑫    时间: 2012-8-10 22:45
?????
作者: 杨文宇    时间: 2012-8-11 00:45
java.lang.IllegalAccessException: Class df.ClassLoaderTest can not access a member of class df.ClassLoaderAttachment with modifiers ""
类加载器不能加载非public 的类。
所以加上public 再试试:
public class ClassLoaderAttachment extends Date{
        public String toString(){
           return "Hello, world!";
        }
}





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2