Java Reflection Method Main Invoke invokeMain(Class mainClass, List args)

Here you can find the source of invokeMain(Class mainClass, List args)

Description

invokeMain.

License

Open Source License

Parameter

Parameter Description
mainClass a java.lang.Class object.
args a java.util.List object.

Exception

Parameter Description

Declaration

public static void invokeMain(Class<?> mainClass, List<String> args) throws Exception 

Method Source Code

//package com.java2s;
/**/*from  ww w . j  a va2 s. c  om*/
 * Copyright (c) 2009 Pyxis Technologies inc.
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA,
 * or see the FSF site: http://www.fsf.org.
 *
 * @author oaouattara
 * @version $Id: $Id
 */

import java.lang.reflect.Method;
import java.util.List;

public class Main {
    /**
     * <p>invokeMain.</p>
     *
     * @param mainClass a {@link java.lang.Class} object.
     * @param args a {@link java.util.List} object.
     * @throws java.lang.Exception if any.
     */
    public static void invokeMain(Class<?> mainClass, List<String> args) throws Exception {
        Method mainMethod = mainClass.getMethod("main", Class.forName("[Ljava.lang.String;"));

        mainMethod.invoke(null, convertToArray(args));
    }

    private static Object convertToArray(List<String> args) {
        String[] array = new String[args.size()];
        args.toArray(array);
        return array;
    }
}

Related

  1. invoke(String property, Object main)
  2. invokeMain(Class clazz, String[] args)
  3. invokeMain(Class fooClass)
  4. invokeMain(ClassLoader classloader, String classname, String[] args)
  5. invokeMain(Method main, String[] args)
  6. invokeMain(String className, String[] args, ClassLoader classLoader)
  7. invokeMain(String... args)