import java.lang.reflect.*;

class InvokeMain
{
    public static void main( String[] args )
    {
        invokeMain( "TestMain", new Object[] { new String[] { "Hello", "World" } } );
    }
    
    public static void invokeMain( String className, Object[] params )
    {
        try
        {
            Class cl = Class.forName( className );
            Class[] mainsParamTypes = new Class[] { String[].class };
            
            Method mainMethod = cl.getMethod( "main", mainsParamTypes );
            if( !Modifier.isStatic( mainMethod.getModifiers( ) ) )
                System.out.println( "Oops... main is not static!" );
            else if( mainMethod.getReturnType( ) != Void.TYPE )
                System.out.println( "Oops... main doesn't return void!" ); 
            else
                mainMethod.invoke( null, params );
        }
        catch( ClassNotFoundException e ) 
        {
            System.out.println( "Cannot find " + className );
        }
        catch( NoSuchMethodException e )
        {
            System.out.println( "Cannot find main in " + className );
        }
        catch( IllegalAccessException e )
        {
            System.out.println( "Cannot invoke main??? in " + className );
        }
        catch( InvocationTargetException e )
        {
            System.out.println( "main threw an exception" );
            e.getTargetException( ).printStackTrace( );
        }
    }
    
}

class TestMain
{
    public static void main( String[] args )
    {
        System.out.println( "Invoking TestMain" );
        for( int i = 0; i < args.length; i++ )
            System.out.println( args[ i ] );
        
        throw new RuntimeException( "Stupid throws!" );    
    }
}    
