Java reflection code sample to call main method of a class.

Class RandomClass {
    public static void main(String args[]) {
      for (String arg : args) {
        System.out.println(arg);
      }
    }
}

Reflection example

try {
    final Class<?> clazz = Class.forName("RandomClass");
    final Method method = clazz.getMethod("main", String[].class);

    final Object[] args = new Object[1];
    args[0] = new String[] { "1", "2"};
    method.invoke(null, args);
} catch (final Exception e) {
    e.printStackTrace();
}