title: Java Reflection: Call method from Interface name
body: <p>I have have a name of an interface and I want to invoke a method defined by its concrete implemented class. So I took help of Java Reflection. </p>
  
  <p>The interface:</p>
  
  <pre><code>package tsb.learning.reflection;
  
  public interface IAnyThing {
  
      void doSomething();
  }
  </code></pre>
  
  <p>It's implemented class:</p>
  
  <pre><code>package tsb.learning.reflection;
  
  public class AnyThing implements IAnyThing {
  
      public void doSomething() {
          System.out.println("JYM");
      }
  }
  </code></pre>
  
  <p>The implementation of <code>InvocationHandler</code>:</p>
  
  <pre><code>package tsb.learning.reflection;
  
  import java.lang.reflect.InvocationHandler;
  import java.lang.reflect.Method;
  
  public class AnyInvocationHandler implements InvocationHandler {
  
      @Override
      public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
          return method.invoke(proxy, args);
      }
  }
  </code></pre>
  
  <p>And the Controller:</p>
  
  <pre><code>package tsb.learning.reflection;
  
  import java.lang.reflect.InvocationHandler;
  import java.lang.reflect.Proxy;
  
  public class Controller {
  
      /**
       * @param args
       * @throws ClassNotFoundException
       */
      public static void main(String[] args) throws ClassNotFoundException {
          String interfaceName = "tsb.learning.reflection.IAnyThing";
          ClassLoader classLoader = Class.forName(interfaceName).getClassLoader();
          Class&lt;?&gt;[] interfaces = new Class&lt;?&gt;[] { Class.forName(interfaceName) };
          InvocationHandler handler = new AnyInvocationHandler();
          IAnyThing anyThing = (IAnyThing) Proxy.newProxyInstance(classLoader, interfaces, handler);
          anyThing.doSomething();
      }
  }
  </code></pre>
  
  <p>But it is not working and I am getting the following exception:</p>
  
  <pre><code>Caused by: java.lang.reflect.InvocationTargetException
      at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      at java.lang.reflect.Method.invoke(Unknown Source)
      at tsb.learning.reflection.AnyInvocationHandler.invoke(AnyInvocationHandler.java:10)
      at $Proxy0.doSomething(Unknown Source)
      at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      at java.lang.reflect.Method.invoke(Unknown Source)
      at tsb.learning.reflection.AnyInvocationHandler.invoke(AnyInvocationHandler.java:10)
      at $Proxy0.doSomething(Unknown Source)
      at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      at java.lang.reflect.Method.invoke(Unknown Source)
      at tsb.learning.reflection.AnyInvocationHandler.invoke(AnyInvocationHandler.java:10)
      at $Proxy0.doSomething(Unknown Source)
      at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      at java.lang.reflect.Method.invoke(Unknown Source)
      at tsb.learning.reflection.AnyInvocationHandler.invoke(AnyInvocationHandler.java:10)
  </code></pre>
  
  <p>The exception is printing in console in loop, I need to stop the program.</p>
  
  <p>Any information will be very helpful to me.</p>