-1

I just created the following minimalistic testcase:

package testcase;

public class Main
{

    public static void main( String[] args )
        throws Throwable
    {
        if ( args.length == 0 )
            Main.class.getMethod( "main", String[].class ).invoke( null, new String[] { "test" } );
    }

}

It should just run, with no output and no exception. The main method should be calling itself using reflection. However I get the following exception:

Exception in thread "main" java.lang.IllegalArgumentException: argument type mismatch
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at testcase.Main.main(Main.java:10)

And I cannot figure out, why...

Didier L
  • 18,905
  • 10
  • 61
  • 103
Steffen Heil
  • 4,286
  • 3
  • 32
  • 35
  • When you compile this you get a warning from the compiler which gives you a hint about what's wrong. – Jesper Mar 21 '16 at 08:23

2 Answers2

8

Use

public static void main(String[] args) throws Throwable {
    if (args.length == 0)
        Main.class.getMethod("main", String[].class)
                  .invoke(null, new Object[] {new String[] { "test" }});
}

The problem is that invoke has vararg parameter which could be either array or plain list of objects and Java arrays are covariant. So .invoke(null, new String[] { "test" }) is interpreted by compiler in the same way as .invoke(null, new Object[] { "test" }). You should have compiler warning about this ambiguity.

Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334
1

Cast new String[] { "test" } in a new Object[] {}.

Marievi
  • 4,951
  • 1
  • 16
  • 33