Java Class Loader addFileToSystemClassLoader(String s)

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

Description

add File To System Class Loader

License

Open Source License

Declaration

public static void addFileToSystemClassLoader(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 {
    private static final Class[] parameters = new Class[] { URL.class };

    public static void addFileToSystemClassLoader(String s) throws IOException {
        File f = new File(s);
        addFileToSystemClassLoader(f);//from ww  w.j a  v a  2 s .  co  m
    }

    public static void addFileToSystemClassLoader(File f) throws IOException {
        addURLToSystemClassLoader(f.toURI().toURL());
    }

    public static void addURLToSystemClassLoader(URL u) throws IOException {
        URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
        Class<URLClassLoader> 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. addDirectoryToClassLoader(ClassLoader loader, File directory)
  2. createClassloader(File libDir, ClassLoader cl)
  3. createClassLoader(File root)
  4. createClassLoader(final String[] cpEntries)
  5. createClassLoader(String fileName)