One app, multiple main()'s

Hi;

I have a single project that has about 18 main methods in it. How can I run the created exe (it's a console app) and tell it which main to use as "the" main

thanks - dave


Answer this question

One app, multiple main()'s

  • Gohan222

    That still won't let me pick which main to run.

    thanks - dave

  • Golf4Fun

    Hi;

    I'm not building, just running it. I don't want to recompile each time I need a different main in the exe. And that would never work when telling customers using it as they don't even have the compiler.

    thanks - dave


  • Christatos

    Hi;

    What I want is a little different. I have the .exe and I want to run the .exe using a given main - like you can do with jar files under java. I don't want to require a recompile each time the main program is changed.

    thanks - dave

  • Dreamtec

    Hi;

    This works, except I have a command line for some of these main programs. So for those I have to pass part of the command line through.

    Isn't there any way to start a program and tell it what main to use Java has had this from the begining so you would think .net would have it too.

    If not I guess I'll have to do as you showed - yuck.

    thanks - dave

  • charlie_niner

    Refer my above post -

    If you are building from command line you can use /m[ain]:<class> option of vjc.exe.

    If you are building using VS, you can set the "Startup Object" property in the Project Properties->Application page.

    Thanks,
    Gautam


  • Tan Silliksaar

    Hi David,
    As of now I don't know if this scenarios is supported in any .net language. I can also only think of the workaround suggested by Kazuya. I will get back you you by tommorow with details.

    Thanks.

  • ashishth

    If you are building from command line you can use /m[ain]:<class> option of vjc.exe.

    If you are building using VS, you can set the "Startup Object" property in the Project Properties->Application page.

    Thanks,
    Gautam

  • Yogesh Ranade

    Hi;

    I can't do that because I am running this from nant so I need to have a command line that sets it. like "java net.windward.xmlreport.RunReport" to run that main in a jar.

    thanks - dave

  • David Teitlebaum MSFT

    Try refelction to do it like the following.

    Class.forName(className).getMethod("main", new Class[] { String[].class }).invoke(null, new Object[] { stringArray });

    Kazuya Ujihara

  • Val Maltsev

    Add the following class and set it startup object.

    class Program
    {
     public static void main(String[] args) throws Throwable
     {
      if (args.length < 1)
      {
       System.err.println("Missing arguments.");
       System.exit(1);
      }
      String className = args[0];
      String[] stringArray = new String[args.length-1];
      System.arraycopy(args, 1, stringArray, 0, args.length - 1);
      Object ret;
      try
      {
       ret = Class.forName(className).getMethod("main", new Class[] { String[].class }).invoke(null, new Object[] { stringArray });
      }
      catch (java.lang.reflect.InvocationTargetException ite)
      {
       throw(ite.getTargetException());
      }
      if (ret == null || !(ret instanceof Integer))
       System.exit(0);
      System.exit(((Integer)ret).intValue());
     }
    }

    You can handle '-D' and '-cp' option of JavaVM using this approach.

  • ManchesterMike

    Set 'java' to your assembly name.
    Of course, have known it's a crazy ideal.


  • One app, multiple main()'s