A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 吴小铁你好 中级黑马   /  2012-6-8 22:44  /  1583 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. package pack;

  2. import java.io.*;
  3. import java.lang.reflect.*;
  4. public class MyClassLoader extends ClassLoader
  5. {
  6.         private String path = null;
  7.         public MyClassLoader(String path) throws Exception//检查文件是否存在
  8.         {
  9.                 File f = new File(path);
  10.                 if(!f.isDirectory())
  11.                 {
  12.                         throw new RuntimeException(path + " is not a directory");
  13.                 }
  14.                 this.path = path;
  15.         }
  16.        
  17.         public Class findClass(String name)
  18.         {
  19.                 try
  20.                 {
  21.                         File f = new File(path,name.substring(name.lastIndexOf('.')+1) + ".class");
  22.                         FileInputStream fis = new FileInputStream(f);
  23.                         ByteArrayOutputStream bos = new ByteArrayOutputStream();
  24.                         cypher(fis,bos);
  25.                         byte [] buf = bos.toByteArray();
  26.                         fis.close();
  27.                         bos.close();
  28.                         return defineClass(name,buf,0,buf.length);
  29.                 }catch(Exception e)
  30.                 {
  31.                         try {
  32.                                 throw new ClassNotFoundException(name + " is not found!");
  33.                         } catch (ClassNotFoundException e1) {
  34.                                 // TODO Auto-generated catch block
  35.                                 e1.printStackTrace();
  36.                         }
  37.                 }
  38.                 return null;
  39.         }

  40.        
  41.         public static void cypher(InputStream istream,OutputStream ostream) throws Exception
  42.         {
  43.                
  44.                 int b = 0;
  45.                 while((b = istream.read()) != -1)
  46.                 {
  47.                         ostream.write(((byte)b) ^ 0xff);
  48.                 }
  49.         }

  50.         public static void main(String [] args) throws Exception
  51.         {

  52.                 if(!args[0].endsWith("class"))
  53.                 {
  54.                         ClassLoader loader = new MyClassLoader(args[1]);
  55.                         Class cls = loader.loadClass(args[0]);
  56.                        
  57.                        
  58.                         //让自定义类继承Date类
  59.                         System.out.println(cls.getClassLoader().getClass().getName());
  60.                                 java.util.Date d = (java.util.Date)cls.newInstance();
  61.                                 System.out.println(d.toString());
  62.                                
  63.                                
  64.                         Method m = cls.getMethod("test");
  65.                         m.invoke(cls.newInstance());
  66.                         return;
  67.                 }
  68.                 else
  69.                 {               
  70.                         FileInputStream fis = new FileInputStream(args[0]);               
  71.                         File f = new File(args[1], new File(args[0]).getName());//不用检查目录最后是否有目录分割符
  72.                         FileOutputStream fos = new FileOutputStream(f);               
  73.                         cypher(fis,fos);
  74.                         fis.close();
  75.                         fos.close();
  76.                 }
  77.         }
  78. }

复制代码
为什么运行结果报异常,找很久都找不出来啊?
  1. Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
  2.         at pack.MyClassLoader.main(MyClassLoader.java:56)
复制代码

评分

参与人数 1技术分 +1 收起 理由
赵志勇 + 1

查看全部评分

1 个回复

倒序浏览
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0

       at pack.MyClassLoader.main(MyClassLoader.java:56)

楼主不会看不懂这个异常吧,代码56行数组下表越界异常

你main方法的args[]参数根本没有传任何东西,
if(!args[0].endsWith("class"))
你这样搞,当然会抛异常了

评分

参与人数 1技术分 +1 收起 理由
赵志勇 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马