Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Reflection and calling main()

1 view
Skip to first unread message

Bill Tschumy

unread,
Jan 16, 2003, 12:06:04 PM1/16/03
to
I am trying to load an arbitrary class (known only by its name) and call its
main() method (assuming it has one).

When doing a method lookup using reflection you have to specify an array of
classes corresponding to the parameter types of the method.

Here is the skeleton of my code:

Class turtleClass = loader.findAndResolveClass(classname);

// just an empty array for now
Object[] arguments = new Object[] {new String[0]};
Class[] parameterTypes = new Class[] {arguments[0].getClass()};

try
{
// THIS CALL TO getMethod FAILS!!
Method mainMethod = turtleClass.getMethod("main", parameterTypes);
mainMethod.invoke(null, arguments);
}
catch (NoSuchMethodException e)
{
e.printStackTrace();
}
catch (InvocationTargetException e)
{
e.printStackTrace();
}

When executing the code I get the following exception:

java.lang.VerifyError: (class: Arcs, method: main signature:
([Ljava/lang/String;)V) Incompatible object argument for function call
at java.lang.Class.getMethod0(Native Method)
at java.lang.Class.getMethod(Class.java:883)
at com.otherwise.jurtle.Display.runTurtleCode(Display.java:114)
at
com.otherwise.jurtle.DisplayPane.runTurtleCode(DisplayPane.java:99)
The getMethod() call is failing with incompatible arguments. Anyone have any
thoughts as to the problem?

Kenny MacLeod

unread,
Jan 19, 2003, 5:39:11 AM1/19/03
to
In article <0001HW.BA4C429C...@news.houston.sbcglobal.net>,
bi...@otherwise.com says...

> // just an empty array for now
> Object[] arguments = new Object[] {new String[0]};
> Class[] parameterTypes = new Class[] {arguments[0].getClass()};
>
> try
> {
> // THIS CALL TO getMethod FAILS!!
> Method mainMethod = turtleClass.getMethod("main", parameterTypes);
> mainMethod.invoke(null, arguments);
> }

Replace the first line of your code with:

Object[] arguments = new String[0];

and you're there.


kenny

0 new messages