java.lang.ClassLoader

Kresten Krab Thorup krab@eos.dk
Thu Apr 27 08:58:00 GMT 2000


> Veena Murthy wrote:
>> Hi,
>> Can anyone point me to code examples for using java.lang.ClassLoader.
>
The usual scenario is to simply create an instance of URLClassLoader,
passing in an array or URL's that is the class path. Here's a blurp
illustrating how to start a new "application" from inside Java. To use
it, create an AppRunner and pass it to a fresh Thread.
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
public class AppRunner implements Runnable
{
 String mainClass = null;
 String[] mainArgs = null;
 URL[] classPath = null;
 
 public AppRunner (String mainClass, String[] mainArgs, URL[]
classpath)
 {
 this.mainClass = mainClass;
 this.mainArgs = mainArgs;
 this.classPath = classpath;
 }
 public void run ()
 {
 try {
 ClassLoader loader = java.net.URLClassLoader(classPath);
 Class klass = loader.loadClass (mainClass);
 
 Class[] atype = new Class[1];
 atype[0] = (new String[0]).getClass();
 
 Method mainMethod = klass.getDeclaredMethod ("main", atype);
 
 Object[] a = new Object[1];
 a[0] = mainArgs;
 
 mainMethod.invoke (null, a);
 } catch (Throwable ex) {
 ex.printStackTrace ();
 }
 }
}
-- 
Kresten Krab Thorup, Partner
Eastfork Object Space (EOS), Margrethepladsen 3, 8000 Århus C, Denmark
Tel: +45 8732 8787 / Fax: +45 8732 8788 / Mob: +45 2343 4626


More information about the Java mailing list

AltStyle によって変換されたページ (->オリジナル) /