Java Reflection Method Main Invoke invokeMain(String... args)

Here you can find the source of invokeMain(String... args)

Description

invoke Main

License

Open Source License

Declaration

public static void invokeMain(String... args) 

Method Source Code

//package com.java2s;
/**//from  w  w w  .j  a v a 2  s  .c  o  m
 * Copyright (c) 2014-2015 by Wen Yu.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Any modifications to this file must keep this entire header intact.
 */

import java.util.Arrays;

import java.lang.reflect.Method;

public class Main {
    public static void invokeMain(String... args) {
        try {
            Class<?> c = Class.forName(args[0]);
            Class<String[]> argTypes = String[].class;
            Method main = c.getDeclaredMethod("main", argTypes);
            Object mainArgs = Arrays.copyOfRange(args, 1, args.length);
            System.out.format("invoking %s.main()%n", c.getName());
            main.invoke(null, mainArgs);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

Related

  1. invokeMain(Class fooClass)
  2. invokeMain(Class mainClass, List args)
  3. invokeMain(ClassLoader classloader, String classname, String[] args)
  4. invokeMain(Method main, String[] args)
  5. invokeMain(String className, String[] args, ClassLoader classLoader)