Here you can find the source of addDirectory(File directory)
public static void addDirectory(File directory)
//package com.java2s; //License from project: Open Source License import java.io.File; import java.io.FileFilter; import java.lang.reflect.Method; import java.net.URL; import java.net.URLClassLoader; public class Main { private static final URLClassLoader systemClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader(); public static void addDirectory(File directory) { if (directory.isDirectory()) { File[] jars = directory.listFiles(new FileFilter() { @Override/*w w w . java 2s . c o m*/ public boolean accept(File pathname) { return pathname.isFile() && pathname.getName().endsWith(".jar"); } }); for (File jar : jars) { add(jar); } } } public static void add(URL url) { try { Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class); method.setAccessible(true); method.invoke(systemClassLoader, url); } catch (Exception ex) { // ignore exception } } public static void add(File file) { try { add(file.toURI().toURL()); } catch (Exception ex) { // ignore exception } } }