Take the 2-minute tour ×

This is a continuation of the question posted in: http://stackoverflow.com/questions/194698/how-to-load-a-jar-file-at-runtime

I am uncertain as to how to continue to the method invocation level. From my understanding, from the clazz object, I would used getMethod or getDeclaredMethod to get a Method object from which I would call invoke. Of course, invoke requires an instance. Would that then be what is called doRun in the example code?

Do I need to perform the doRun.run() method call even though I want to execute a different method than main (assuming that it is main on the doRun object that is called with the run invocation)?

Just for more clarification of the original post, I ask: Does doRun.run() start a new thread executing the instance of the class object of type clazz?

Thanks for helping to clear this up for me.

I did look at "how-should-i-load-jars-dynamically-at-runtime" (sorry, only allowed one hyperlink), however this looked to violate the Class.newInstance evilness admonition in the first post I referenced.

share|improve this question
add comment

3 Answers

up vote 2 down vote accepted

Here is some reflection code that doesn't cast to an interface:

public class ReflectionDemo {

  public void print(String str, int value) {

  public static int getNumber() { return 42; }

    Class<?> clazz = ReflectionDemo.class;
    // static call
    Method getNumber = clazz.getMethod("getNumber");
    int i = (Integer) getNumber.invoke(null /* static */);
    // instance call
    Constructor<?> ctor = clazz.getConstructor();
    Object instance = ctor.newInstance();
    Method print = clazz.getMethod("print", String.class, Integer.TYPE);
    print.invoke(instance, "Hello, World!", i);

Writing the reflected classes to an interface known by the consumer code (as in the example) is generally better because it allows you to avoid reflection and take advantage of the Java type system. Reflection should only be used when you have no choice.

share|improve this answer
So, if I understand your comment following the code, with an interface, I know what methods are available and can write code calling the method directly after casting the instance object appropriately. Is this true? –  Todd Aug 24 '09 at 15:01
Of course, that assumes that the original code had been compiled using the interface, not one I create later and attempt to cast the instance to. –  Todd Aug 24 '09 at 15:35
@Todd - yes, you've got it. The interface (or some other strong type implementation) approach is often used with plugins where the code has been written to be instantiated dynamically. If you are doing introspection and invocation on arbitrary classes, this is not an option. –  McDowell Aug 24 '09 at 15:47
@McDowell - I have been unsuccessful in my variant of your code. I added the following method: public void print2(String[] strings){ for(final String string : strings){ System.out.println(string); } } with the following in the main method: Method print2 = clazz.getDeclaredMethod("print2", new Class[]{String[].class}); print2.invoke(instance, new String[]{"test1", "test2"}); This results in: Exception in thread "main" java.lang.IllegalArgumentException: wrong number of arguments Do you have any ideas? –  Todd Aug 24 '09 at 20:05
That last post is too messy to read, I will generate a new post. –  Todd Aug 24 '09 at 20:05
add comment

The code example

ClassLoader loader = URLClassLoader.newInstance(
    new URL[] { yourURL },
Class<?> clazz = Class.forName("mypackage.MyClass", true, loader);
Class<? extends Runnable> runClass = clazz.asSubclass(Runnable.class);
// Avoid Class.newInstance, for it is evil.
Constructor<? extends Runnable> ctor = runClass.getConstructor();
Runnable doRun = ctor.newInstance();

assumes that the class you are loading implements a particular interface Runnable, and therefore it's reasonale to cast to that type using asSubclass() and invoke run().

What do you know about the classes you are loading? Can you assume that they implement a particualr interface? If so adjust the asSubClass() line to reference the interafce you prefer.

Then, yes if you are working with instance methods create an instance using the contructor, ctor in the example.

There is no starting of a thread in the example. Creating a new thread would just have needed a couple of lines more code

Thread myThread = new Thread(doRun);
share|improve this answer
djna - thanks for the response. Unfortunately, I can not assume that I know anything about the class(es) that I may be loading. I have one sample case which may not be indicative of real world usage. As it stands, I don't subclass, get an instance from the constructor, then proceed with using getDeclaredMethod, similar to the post below. Thanks for the other clarification, I was confused about the run invocation, thought that meant a new thread was to start - my bad. –  Todd Aug 24 '09 at 14:59
add comment

Sample Program:

Project Printer:

public class Printer {

    public void display(String printtext)


This project is exported as Printer.jar.

Printer Class has method display() which takes string as input.

Invoking code:

       URL url = new URL("file:Printer.jar"); 
       URLClassLoader loader = new URLClassLoader (new URL[] {url});
       Class<?> cl = Class.forName ("Printer", true, loader);
       String printString = "Print this";
       Method printit = cl.getMethod("display", String.class);
       Constructor<?> ctor = cl.getConstructor(); //One has to pass arguments if constructor takes input arguments.
       Object instance = ctor.newInstance();
       printit.invoke(instance, printString);
       loader.close ();

Output: Print this

share|improve this answer
add comment

Your Answer


