代码目的:利用java中classLoader,加载一个class文件进内存成class实例。package code.loader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;public class ClassLoaderDemo {
    public static void main(String[] args) throws InstantiationException, IllegalAccessException {
        try {
        
            // 测试路径
            String classPath1 = "file:/E:/eclipse/workspace/OK/bin/code/mytest";
            String classPath2 = "file:/E:/eclipse/workspace/OK/bin/code/mytest";
            // 测试类
            String className1 = "Tesst2";
            String className2 = "Tesst2";
            // 创建个URL
            URL url1 = new URL(classPath1);
            // 建立ClassLoader
            ClassLoader loader1 = new URLClassLoader(new URL[] {url1});
            System.out.println(className1);
            // 载入类
            Class c1 = loader1.loadClass(className1);
            System.out.println(c1);
       
            URL url2 = new URL(classPath2);
            ClassLoader loader2 = new URLClassLoader(new URL[] {url2});
            Class c2 = loader2.loadClass(className2);
            System.out.println(c2);
       
            System.out.println("是否相同?" + (c1 == c2));
        }
        catch(ArrayIndexOutOfBoundsException e) {
            System.out.println("路劲不对");
        }
        catch(MalformedURLException e) {
            System.out.println("载入路径错误");
        }
        catch(ClassNotFoundException e) {
            System.out.println("找不到指定的类");
        }
    }
}结果是:找不到指定的类及ClassNotFoundException 的异常,是loader1.loadClass(className1);代码包的异常。
1,检查路劲是对的,否则也通不过new URLClassLoader(new URL[] {url1});这代码吧
2,检查Tesst2.class文件存在
3,问题已经非常清晰,如果实现过类似功能的朋友,可以帮我找下你以前成功的代码帮助我一下,没有代码提供的朋友,可以凭你的经验指出可能出错的地方,我愿意自己尝试你的想法,直到解决问题。先谢谢各位了。补充:这里贴出Test2的代码:
package code.mytest;public class Test2 {
public void sayYouILY(){
System.out.println("I love you!");
}
}

解决方案 »

  1.   

    粗略看了下,类名和路径名都不正确。String classPath1 = "file:/E:/eclipse/workspace/OK/bin/code/mytest";
    应该是:
    String classPath1 = "file:/E:/eclipse/workspace/OK/bin/";
    String className1 = "Tesst2";
    应该是:
    String className1 = "code.mytest.Tesst2";
      

  2.   

    bin目录文件中的就是java对应的class文件(带包名);
    class文件名的话就是从bin的下一级目录开始算起。
    file:/E:/eclipse/workspace/OK/bin/code/mytest/Tesst2.class
      

  3.   

    你有没有尝试过从CMD命令行来运行过带包名的类?
    路径是要从包的根开始指定的。其次,你指定的类名如果是 Tesst2,对于Java来说跟code.mytest.Tesst2是完全不同的两个类。
      

  4.   

    我的class文件是在E:/eclipse/workspace/OK/bin/code/mytest文件夹下的,依照你的写法我改正后还是报同样的错。你看是不是这个功能本来就不能实现的呀?
      

  5.   

    修改如下:
     // 测试路径
                String classPath1 = "file:/E:/eclipse/workspace/OK/bin/";
                String classPath2 = "file:/E:/eclipse/workspace/OK/bin/";
                // 测试类
                String className1 = "code/mytest/Tesst2";
                String className2 = "code/mytest/Tesst2";
    可是依然不可行呀
      

  6.   

    // 测试类
    String className1 = "code.mytest.Tesst2";
    String className2 = "code.mytest.Tesst2";
      

  7.   

    好吧,我也测试了下,也发出来给你参考吧
    package reflect;import java.lang.reflect.*;
    import java.net.*;public class ReflectCreateClass {    public static void main(String[] args) throws Exception {
            // ClassPath
            URL classPath = ReflectCreateClass.class.getResource("/");
            System.out.println("路径:" + classPath);        // ClassLoader
            ClassLoader loader = new URLClassLoader(new URL[] {classPath});
            String className = "reflect.test.Test2"; // 我用的包跟你不一样,内容完全一样的
            
            // Get Class Definition for Reflect
            Class clazz = loader.loadClass(className);
            System.out.println(clazz);
            Method method = clazz.getDeclaredMethod("sayYouILY");
            System.out.println(method);
            
            // Create Object & Invoke Method
            Object obj = clazz.newInstance();
            method.invoke(obj);
        }}