title: Avoid incorrect number of arguments when invoking a Java class main method from Groovy
body: <p>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).</p>
  <p>I have tried using a modified version from the Java reflection tutorial thusly:</p>
  <pre><code>Class&lt;?&gt; c = Class.forName(className)
  Class[] argTypes = new Class[] { String[].class }
  Method main = c.getDeclaredMethod(&quot;main&quot;, argTypes)
  String[] mainArgs = Arrays.copyOfRange(args, 0, args.length)
  main.invoke(null, mainArgs)
  </code></pre>
  <p>This throws an IllegalArgumentException as follows:</p>
  <p>Exception in thread &quot;main&quot; java.lang.IllegalArgumentException: wrong number of arguments</p>
  <p>Other items to note:</p>
  <ul>
  <li><code>argTypes[0]</code> is <code>class [Ljava.lang.String</code></li>
  <li><code>main</code> is <code>public static void AClassName.main(java.lang.String[])</code></li>
  <li><code>mainArgs</code> is <code>java.lang.String[6]</code></li>
  </ul>
  <p>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?</p>