小弟在学习classload,网上找了一个例子
package com.demo.test;public class Test {
public static void main(String[] args)
{
System.out.println("ok");
}
   
}
package com.demo.test;import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.WritableByteChannel;public class MyClassLoader extends ClassLoader {   
    private String fileName;   
  
    public MyClassLoader(String fileName) {   
        this.fileName = fileName;   
    }   
  
    protected Class<?> findClass(String className) throws ClassNotFoundException {   
        Class clazz = this.findLoadedClass(className);   
        if (null == clazz) {   
            try {   
                String classFile = getClassFile(className);   
                FileInputStream fis = new FileInputStream(classFile);   
                FileChannel fileC = fis.getChannel();   
                ByteArrayOutputStream baos = new  
                        ByteArrayOutputStream();   
                WritableByteChannel outC = Channels.newChannel(baos);   
                ByteBuffer buffer = ByteBuffer.allocateDirect(1024);   
                while (true) {   
                    int i = fileC.read(buffer);   
                    if (i == 0 || i == -1) {   
                        break;   
                    }   
                    buffer.flip();   
                    outC.write(buffer);   
                    buffer.clear();   
                }   
                fis.close();   
                byte[] bytes = baos.toByteArray();   
  
                clazz = defineClass(className, bytes, 0, bytes.length);   
            } catch (FileNotFoundException e) {   
                e.printStackTrace();   
            } catch (IOException e) {   
                e.printStackTrace();   
            }   
        }   
        return clazz;   
    }   
   
    private String getClassFile(String name) {   
        StringBuffer sb = new StringBuffer(fileName);   
        name = name.replace('.', File.separatorChar) + ".class";   
        sb.append(File.separator + name);   
        return sb.toString();   
    }   
}  
package com.demo.test;public class MainClassLoader {   
    public static void main(String[] args) {   
        try {   
            MyClassLoader tc = new MyClassLoader("D:/worktemp/Demo/bin/com/demo/test");   
            Class c = tc.findClass("Test");   
            c.newInstance();   
        } catch (ClassNotFoundException e) {   
            e.printStackTrace();    
        } catch (IllegalAccessException e) {   
            e.printStackTrace();   
        } catch (InstantiationException e) {   
            e.printStackTrace();    
        }   
    }   
}  我是想用自己生成的classload去装载Test.class,这个文件的绝对路径是D:/worktemp/Demo/bin/com/demo/test下Test.java文件路径是在D:\worktemp\Demo\src\com\demo\test 下运行时发生这个错误,不知道怎么处理谢谢
Exception in thread "main" java.lang.NoClassDefFoundError: Test (wrong name: com/demo/test/Test)

解决方案 »

  1.   

    Test.java文件路径是在D:\worktemp\Demo\src\com\demo\test 下
    Test.class文件在哪?
      

  2.   

    只要是类名没写对:
     MyClassLoader tc = new MyClassLoader("D:/worktemp/Demo/bin/com/demo/test"); 
    改为
     MyClassLoader tc = new MyClassLoader("D:/worktemp/Demo/bin");然后
    Class c = tc.findClass("Test"); 
    改为
    Class c = tc.findClass("com.demo.test.Test"); LZ那个构造方法就是为了提供一个路径,defineClass中的name规定必须是binary name,binary name大概就是
    一个完整类名的意思吧,说不上来,可以看看JLS或者API文档ClassLoader那里。