This is the mail archive of the
java-discuss@sourceware.cygnus.com
mailing list for the Java project.
Re: java.lang.ClassLoader
- To: Veena Murthy <vmurthy at nortelnetworks dot com>
- Subject: Re: java.lang.ClassLoader
- From: Kresten Krab Thorup <krab at eos dot dk>
- Date: Thu, 27 Apr 2000 19:47:58 +0200
- CC: java-discuss at sourceware dot cygnus dot com
- Organization: Eastfork Object Space
- References: <9A9367D1556AD21182C40000F80930AB026A0568@crchy28b.us.nortel.com>
> 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