mardi 3 août 2021

Avoid incorrect number of arguments when invoking a Java class main method from Groovy

From a Groovy class method I am trying to invoke the main function of a Java class in the default package (I know not optimal but am using legacy code).

I have tried using a modified version from the Java reflection tutorial thusly:

Class<?> c = Class.forName(className)
Class[] argTypes = new Class[] { String[].class }
Method main = c.getDeclaredMethod("main", argTypes)
String[] mainArgs = Arrays.copyOfRange(args, 0, args.length)
main.invoke(null, mainArgs)

This throws an IllegalArgumentException as follows:

Exception in thread "main" java.lang.IllegalArgumentException: wrong number of arguments

Is there a different way of doing this that is needed in Groovy versus Java? Otherwise how to invoke main from a default package class?





Aucun commentaire:

Enregistrer un commentaire