Java ClassLoader addDirectory(File directory)

Here you can find the source of addDirectory(File directory)

Description

add Directory

License

Open Source License

Declaration

public static void addDirectory(File directory) 

Method Source Code


//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
        }
    }
}

Related

  1. add(File file)
  2. AddFile(File f)
  3. addFile(File file)
  4. addFile(String s)
  5. createDir(String pathName)