本帖最后由 高鑫 于 2012-8-10 20:54 编辑
- //存储文件名ClassLoaderAttachment.java
- package df;
- import java.util.*;
-
- class ClassLoaderAttachment extends Date{
- public String toString(){
- return "Hello, world!";
- }
- }
复制代码- //存储文件名MyClassLoader.java
- package df;
- import java.io.*;
- class MyClassLoader extends ClassLoader{
- public static void main(String args[])throws Exception{
- String srcPath=args[0];
- String destDir=args[1];
- FileInputStream fis=new FileInputStream(srcPath);
- String destFileName=srcPath.substring(srcPath.lastIndexOf('\\')+1);
- String destPath=destDir+"\\"+destFileName;
- FileOutputStream fos=new FileOutputStream(destPath);
- cypher(fis,fos);
- fis.close();
- fos.close();
- }
- private static void cypher(InputStream ips,OutputStream ops)throws Exception{
- int b=-1;
- while((b=ips.read())!=-1){
- ops.write(b^0xff);
- }
- }
- private String classDir;
- @Override
-
- protected Class<?>findClass(String name) throws ClassNotFoundException{
- String classFileName=classDir+"\\"+name+".class";
- try{
-
- FileInputStream fis=new FileInputStream(classFileName);
- ByteArrayOutputStream bos=new ByteArrayOutputStream();
- cypher(fis,bos);
- fis.close();
-
- System.out.println("aaaa");
-
- byte[] bytes=bos.toByteArray();
- return defineClass(bytes,0,bytes.length);
- }
- catch(Exception e){
- e.printStackTrace();
- }
- return null;
- }
- public MyClassLoader(){
-
- }
- public MyClassLoader(String classDir){
- this.classDir=classDir;
- }
- }
复制代码- //文件名ClassLoaderTest.java
- package df;
- import java.util.Date;
- class ClassLoaderTest{
- public static void main(String args[]) throws ClassNotFoundException, InstantiationException, IllegalAccessException{
- Class<?> clazz=new MyClassLoader("itcastlib").loadClass("ClassLoaderAttachment");
- Date d1=(Date) clazz.newInstance();
- System.out.println(d1);
-
- }
- }
复制代码 这个是老师的例题,第一个程序存储文件名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 ""
请问哪里出了问题?? |
|