Java ClassPath Add addToClassPath(String s)

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

Description

add To Class Path

License

Open Source License

Declaration

public static void addToClassPath(String s) 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 {
    public static void addToClassPath(String s) throws IOException {
        File f = new File(s);
        addToClassPath(f);/*w  ww . j a  v a 2  s. c  o m*/
    }

    public static void addToClassPath(File f) throws IOException {
        addToClassPath(f.toURL());
    }

    public static void addToClassPath(URL u) throws IOException {

        URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
        Class sysclass = URLClassLoader.class;

        try {
            Method method = sysclass.getDeclaredMethod("addURL", new Class[] { URL.class });
            method.setAccessible(true);
            method.invoke(sysloader, new Object[] { u });
        } catch (Throwable t) {
            t.printStackTrace();
            throw new IOException("Error, could not add URL to system classloader");
        }

    }
}

Related

  1. addDirToClasspath(File directory)
  2. addPathToClassPath(String s)
  3. addToClasspath(File f)
  4. addToClassPath(File file)
  5. addToClasspath(File file)
  6. addToClassPath(Vector cpV, String dir)