Java ClassPath Add addDirToClasspath(File directory)

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

Description

Adds the jars in the given directory to classpath

License

Open Source License

Parameter

Parameter Description
directory a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static void addDirToClasspath(File directory) throws IOException 

Method Source Code


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

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;

public class Main {
    /**/*from w  ww  .ja  v  a2s .  c o  m*/
     * Adds the jars in the given directory to classpath
     * 
     * @param directory
     * @throws IOException
     */
    public static void addDirToClasspath(File directory) throws IOException {

        // Iterate over files and add to classpath
        for (File file : directory.listFiles()) {
            addURL(file.toURI().toURL());
        }
    }

    /**
     * Add URL to CLASSPATH
     * 
     * @param URL
     * @throws IOException
     */
    public static void addURL(URL newURL) throws IOException {

        // Get system class loader
        URLClassLoader sysLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();

        // Test the newURL is not alread loaded
        for (URL url : sysLoader.getURLs()) {

            // compare URL
            if (url.toString().equalsIgnoreCase(newURL.toString())) {

                // already loaded
                System.out.println("URL " + newURL + " is already in the CLASSPATH");
                return;
            }
        }

        // load newURL
        Class<URLClassLoader> sysclass = URLClassLoader.class;
        try {

            // In
            Method method = sysclass.getDeclaredMethod("addURL", new Class[] { URL.class });
            method.setAccessible(true);
            method.invoke(sysLoader, new Object[] { newURL });

        } catch (Throwable t) {
            t.printStackTrace();

            throw new IOException("Error, could not add URL to system classloader");
        }
    }
}

Related

  1. addClassPath(File filePath)
  2. addClasspath(String path)
  3. addClassPath2ClassLoader(ClassLoader cl, String path)
  4. addClasspathEntries(Collection cpEntries)
  5. addClassPathItems(String[] cpItems)
  6. addDirToClasspath(File directory)
  7. addPathToClassPath(String s)
  8. addToClasspath(File f)
  9. addToClassPath(File file)