Java ClassLoader createDirectoryLoader(String directory)

Here you can find the source of createDirectoryLoader(String directory)

Description

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

License

Open Source License

Parameter

Parameter Description
directory - the path of the directory the ClassLoader should load from

Exception

Parameter Description
IOException an exception
URISyntaxException an exception

Return

a ClassLoader that can be used to load classes and resources from files in the specified directory

Declaration

public static ClassLoader createDirectoryLoader(String directory) throws URISyntaxException, IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

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 {
    /**/*from   ww  w.j a  v  a2 s . com*/
     * 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

  1. addDirectory(File directory)
  2. AddFile(File f)
  3. addFile(File file)
  4. addFile(String s)
  5. createDir(String pathName)
  6. CreateLogbackXML(OutputStream out)
  7. findFile(String filename)
  8. hasImageFile(String name)
  9. hasLogDatePatternMatcher(final String line)