Java ClassPath Add addClassPathItems(String[] cpItems)

Here you can find the source of addClassPathItems(String[] cpItems)

Description

Adds the items to the classPath of the system classLoader

License

Open Source License

Parameter

Parameter Description
cpItems classPath items to be added

Exception

Parameter Description
MalformedURLException an exception

Return

extended URLClassLoader

Declaration

public static URLClassLoader addClassPathItems(String[] cpItems) throws MalformedURLException 

Method Source Code


//package com.java2s;
import java.io.File;

import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;

public class Main {
    /**/*  ww  w  .  j a  va2  s  . co m*/
     * Adds the items to the classPath of the system classLoader
     * 
     * @param cpItems
     *            classPath items to be added
     * @return extended URLClassLoader
     * 
     * @throws MalformedURLException
     */
    public static URLClassLoader addClassPathItems(String[] cpItems) throws MalformedURLException {
        return addClassPathItems(ClassLoader.getSystemClassLoader(), cpItems);
    }

    /**
     * Adds to the give classLoader the class path items
     * 
     * @param classLoader
     *            any class loader, you can use
     *            ClassLoader.getSystemClassLoader()
     * @param cpItems
     *            paths to items to be added
     * @return the extended URLClassLoader
     * 
     * @throws MalformedURLException
     */
    public static URLClassLoader addClassPathItems(ClassLoader classLoader, String[] cpItems)
            throws MalformedURLException {
        URL[] urls = new URL[cpItems.length];
        for (int i = 0; i < cpItems.length; ++i) {
            File f = new File(cpItems[i]);
            urls[i] = f.toURI().toURL();

        }
        return new URLClassLoader(urls, classLoader);
    }
}

Related

  1. addBundleToClassPath(String bundleId)
  2. addClassPath(File filePath)
  3. addClasspath(String path)
  4. addClassPath2ClassLoader(ClassLoader cl, String path)
  5. addClasspathEntries(Collection cpEntries)
  6. addDirToClasspath(File directory)
  7. addDirToClasspath(File directory)
  8. addPathToClassPath(String s)
  9. addToClasspath(File f)