|
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
public class BootstrapRunner {
/**
* Assuming BoostrapRunner.class is in this directory and all your other classes are in
* jarfiles or directories under ./lib, you launch your app as shown below.
*
* java -classpath . BootstrapRunner my.MainClass ./lib
*
* You can put everything in lib if BootstrapRunner is the only classfile ... otherwise
* the other classes will get loaded by the system classloader, which could cause problems
* because classes in the system classloader callstack can't see stuff in the jarfiles.
*/
public static void main(String[] args) {
try {
PatchClassLoader pcl = new PatchClassLoader(new File(args[1]), BootstrapRunner.class.getClassLoader());
Class cla = pcl.loadClass(args[0]);
Method main = cla.getMethod("main", new Class[] { String[].class });
main.invoke(null, new Object[] { args } );
} catch (Throwable t) {
t.printStackTrace();
}
}
}
class PatchClassLoader extends URLClassLoader {
public PatchClassLoader(File patchDir, ClassLoader parent) {
super(expand(patchDir), parent);
}
private static URL[] expand(File patchDir) {
try {
File[] files = patchDir.getCanonicalFile().listFiles();
if (files != null) {
URL[] urls = new URL[files.length];
for (int i = 0; i < files.length; i++)
urls[i] = files[i].toURL();
return urls;
} else {
return new URL[] {};
}
} catch (IOException ioe) {
throw new Error("Never happens, heh heh.");
}
}
}
|
java
|
4/11/2003 5:17 PM
rob
(Modified 4/11/2003 7:14 PM)
|
New Window
Move/Edit
|