This is the mail archive of the mailing list for the Java project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: java.lang.ClassLoader

> 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;

public class AppRunner implements Runnable
  String mainClass = null;
  String[] mainArgs = null;
  URL[] classPath = null;
  public AppRunner (String mainClass, String[] mainArgs, URL[]
    this.mainClass = mainClass;
    this.mainArgs  = mainArgs;
    this.classPath = classpath;

  public void run ()
    try {
      ClassLoader loader =;
      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

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]