The following program creates an instance of the class you specify and then invokes any method with a single parameter of type object;
import java.lang.reflect.*;
class InvokeMethod {
public static void main( String ... args ) throws
ClassNotFoundException,
NoSuchMethodException,
IllegalAccessException,
InvocationTargetException,
InstantiationException {
String clazz = args[0]; // which class to create
String method = args[1]; // which method to invoke
String param = args[2]; // parameter to that method
Class c = Class.forName(clazz);
Method m = c.getMethod(method, Object.class);
Object o = c.newInstance();
Object result = m.invoke( o, param );
System.out.printf("%s.%s(%s)=%s", clazz, method, param, result );
}
}
example output:
$ java InvokeMethod java.util.ArrayList add "Hello world"
java.util.ArrayList.add(Hello world)=true
$ java InvokeMethod java.lang.String valueOf "Hello world"
java.lang.String.valueOf(Hello world)=Hello world