加载外部类

来源:互联网 发布:增强for循环遍历数组 编辑:程序博客网 时间:2024/05/17 07:37

public static void loadClassOuter() throws Exception {
        // 外部类路径
        File file = new File("D://Workspaces//MyEclipse 8.6//workone//Root//WEB-INF//classes");
        try {
            // Convert File to a URL
            URL url = file.toURL();
            URL[] urls = new URL[] { url };

            // Create a new class loader with the directory
            ClassLoader cl = new URLClassLoader(urls);

            // Load in the class; MyClass.class should be located in
            // the directory file:/c:/myclasses/com/mycompany
            Class cls = cl.loadClass("com.sohu.Node");
            Field[] fields = cls.getDeclaredFields();
            for (Field f : fields) {
                System.out.println(f.getName());
            }
        } catch (MalformedURLException e) {
        } catch (ClassNotFoundException e) {
        }

    }

原创粉丝点击