title: Java argument type mismatch while reflection
body: <p>I am trying to run some method I load from another jar, that returns an integer, and then I want to return it to another class and pass it to my logic.</p>
  
  <p>I made my method loading class pretty simple like this:</p>
  
  <pre><code>public class ModuleLoader {
  
      private Class&lt;?&gt; cls;
  
      public void initializeCommandModule(Module m) throws Exception {
          URL url = this.getURL(m.getJar());
          this.cls = this.loadClass(m.getMainClass(), url);
      }
  
      public int execute(Module m, ArrayList&lt;String&gt; args) throws Exception {
          Method method = this.cls.getDeclaredMethod("execute", ArrayList.class);
          return (int) method.invoke(this.cls.newInstance(), 1);
      }
  
      public int respond(ArrayList&lt;String&gt; args) throws Exception {
          Method method = this.cls.getDeclaredMethod("response", ArrayList.class);
          return (int) method.invoke(this.cls.newInstance(), 1);
      }
  
      private Class&lt;?&gt; loadClass(String cls, URL url) throws ClassNotFoundException, IOException {
          URLClassLoader loader = new URLClassLoader(new URL[]{url});
          Class&lt;?&gt; toReturn = loader.loadClass(cls);
          loader.close();
  
          return toReturn;
      }
  
      private URL getURL(String jar) throws MalformedURLException {
          return new File(jar).toURI().toURL();
      }
  
  }
  </code></pre>
  
  <p>Take a look at the <code>execute(Module m, ArrayList&lt;String&gt; args)</code> method, this line throws the error:</p>
  
  <pre><code>    return (int) method.invoke(this.cls.newInstance());
  </code></pre>
  
  <p>The jar library I load looks like this:</p>
  
  <pre><code>public class Test {
  
      public int execute(ArrayList&lt;String&gt; i) {
          System.out.println("Hello world!");
          return 0;
      }
  
  }
  </code></pre>
  
  <p>Why when I run that method I get the following exception thrown?</p>
  
  <pre><code>Exception in thread "main" java.lang.IllegalArgumentException: argument type mismatch
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      at java.lang.reflect.Method.invoke(Unknown Source)
      at ben.console.modules.ModuleLoader.execute(ModuleLoader.java:24)
      at ben.console.CommandProcessor.process(CommandProcessor.java:37)
      at ben.console.Console.listen(Console.java:25)
      at ben.console.Console.main(Console.java:31)
  </code></pre>
  
  <p>Thanks in advice!</p>