steamlined classpathhacker
parent
65da10a43c
commit
560be41c72
|
@ -5,51 +5,30 @@ import java.lang.reflect.Method;
|
|||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
|
||||
/**
|
||||
* Useful class for dynamically changing the classpath, adding classes during runtime.
|
||||
/** Useful class for dynamically changing the classpath, adding classes during runtime.
|
||||
* @author unknown
|
||||
*/
|
||||
public class ClasspathHacker {
|
||||
/**
|
||||
* Parameters of the method to add an URL to the System classes.
|
||||
*/
|
||||
private static final Class<?>[] parameters = new Class[]{URL.class};
|
||||
|
||||
/**
|
||||
* Adds a file to the classpath.
|
||||
* @param s a String pointing to the file
|
||||
* @throws IOException
|
||||
*/
|
||||
public static void addFile(String s) throws IOException {
|
||||
File f = new File(s);
|
||||
addFile(f);
|
||||
}//end method
|
||||
|
||||
/**
|
||||
* Adds a file to the classpath
|
||||
* @param f the file to be added
|
||||
* @throws IOException
|
||||
*/
|
||||
public static void addFile(File f) throws IOException {
|
||||
addURL(f.toURI().toURL());
|
||||
}//end method
|
||||
|
||||
/**
|
||||
* Adds the content pointed by the URL to the classpath.
|
||||
* @param u the URL pointing to the content to be added
|
||||
* @throws IOException
|
||||
*/
|
||||
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");
|
||||
}//end try catch
|
||||
}//end method
|
||||
|
||||
/** Adds a file to the classpath
|
||||
* @param f the file to be added
|
||||
* @throws IOException
|
||||
*/
|
||||
public static void addFile(File f) throws IOException {
|
||||
addURL(f.toURI().toURL());
|
||||
}
|
||||
|
||||
/** Adds the content pointed by the URL to the classpath.
|
||||
* @param u the URL pointing to the content to be added
|
||||
* @throws IOException
|
||||
*/
|
||||
public static void addURL(URL u) throws IOException {
|
||||
try {
|
||||
Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
|
||||
method.setAccessible(true);
|
||||
method.invoke(ClassLoader.getSystemClassLoader(), u);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
throw new IOException("Error, could not add URL to system classloader");
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue