Java ClassPath Add addPathToClassPath(String s)

Here you can find the source of addPathToClassPath(String s)

Description

add Path To Class Path

License

Open Source License

Parameter

Parameter Description
s a parameter

Exception

Parameter Description
Exception an exception

Declaration

static public void addPathToClassPath(String s) throws Exception 

Method Source Code


//package com.java2s;
//  it under the terms of the GNU General Public License as published by      //

import java.io.File;

import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;

public class Main {
    /**//ww w  . j  a v a 2 s  . c  om
     *
     * @param s
     * @throws Exception
     */
    static public void addPathToClassPath(String s) throws Exception {
        File f = new File(s);
        URL u = f.toURI().toURL();
        URLClassLoader urlClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
        Class urlClass = URLClassLoader.class;
        Method method = urlClass.getDeclaredMethod("addURL", new Class[] { URL.class });
        method.setAccessible(true);
        method.invoke(urlClassLoader, new Object[] { u });
    }
}

Related

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