自己写的类加载器- @Override
- protected Class<?> findClass(String name) throws ClassNotFoundException{
- String destPath = Path+"\\"+name+".class";
- try{
- FileInputStream ips = new FileInputStream(destPath);
- ByteArrayOutputStream bos = new ByteArrayOutputStream();
- MyClassLoader.cypher(ips, bos);
- byte[] buf = bos.toByteArray();
- return defineClass(buf,0,buf.length);
- //return super.findClass(name);
- }catch(Exception e){
- //throw new RuntimeException(e);
-
- }
- return super.findClass(name);
- }
- private String Path;
- PersonLoader(){
-
- }
- PersonLoader(String Path){
- this.Path = Path;
- }
复制代码
如果把覆盖的findClass里面的String destPath = Path+"\\"+name+".class";改成String destPath = Path+"\\"+nam;Class clazz = new PersonLoader("D:\\workspace\\高新技术\\lib").loadClass("Person.class");会调用自己的类加载器去加载lib文件夹里面的那份字节码
但是String destPath = Path+"\\"+name+".class";
Class clazz = new PersonLoader("D:\\workspace\\高新技术\\lib").loadClass("Person");就会去调用sun.misc.Launcher$AppClassLoader去加载.还会报错
Exception in thread "main" java.lang.NoClassDefFoundError: Person (wrong name: cn/hmm/classLoadTest/Person)
感觉好神奇啊,求大神解答一下
|