Java ClassLoader AddFile(File f)

Here you can find the source of AddFile(File f)

Description

Add File

License

Apache License

Declaration

public static void AddFile(File f) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Apache 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 {
    private static final Class[] parameters = { URL.class };

    public static void AddFile(File f) throws IOException {
        AddURL(f.toURL());//w w w  . ja  v  a  2s.  c  om
    }

    public static void AddURL(URL u) throws IOException {
        URLClassLoader sysloader = (URLClassLoader) ClassLoader
                .getSystemClassLoader();
        Class sysclass = URLClassLoader.class;
        try {
            Method method = sysclass
                    .getDeclaredMethod("addURL", parameters);
            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. add(File file)
  2. addDirectory(File directory)
  3. addFile(File file)
  4. addFile(String s)
  5. createDir(String pathName)
  6. createDirectoryLoader(String directory)