This method returns a new ClassLoader object that can be used to load classes from files contained by the specified directory. - Java Reflection

Java examples for Reflection:Class Loader

Description

This method returns a new ClassLoader object that can be used to load classes from files contained by the specified directory.

Demo Code


//package com.java2s;
import java.io.File;
import java.io.IOException;

import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Collection;

public class Main {
    /**/*w  w  w . j  a  v  a  2s.c  om*/
     * This method returns a new ClassLoader object that can be used to load classes from files contained by the specified
     * directory.
     * @param directory - the path of the directory the ClassLoader should load from
     * @return a ClassLoader that can be used to load classes and resources from files in the specified directory
     * @throws IOException
     * @throws URISyntaxException
     * @throws IOException 
     * @see ClassLoader
     */
    public static ClassLoader createDirectoryLoader(String directory)
            throws URISyntaxException, IOException {
        Collection<URL> urls = new ArrayList<URL>();
        File dir = new File(directory);
        File[] files = dir.listFiles();
        for (File f : files) {
            System.out.println(f.getCanonicalPath());
            urls.add(f.toURI().toURL());
        }

        return URLClassLoader
                .newInstance(urls.toArray(new URL[urls.size()]));
    }
}

Related Tutorials