此方法返回一个新的ClassLoader对象,该对象可用于从指定目录包含的文件中加载类。
//package com.nowjava; import java.io.File; import java.io.IOException;//N o w J a v a . c o m 提 供 import java.net.URISyntaxException; import java.net.URL; import java.net.URLClassLoader; import java.util.ArrayList; import java.util.Collection; public class Main { /** * 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) {